Skip to content
Go back

Using Git Worktree

With the git clone or git init command, by default only one main worktree is created. The worktree is linked to the same directory that you cloned your repository into. You can think of a worktree as the working directory of your repository, which reflects the currently selected branch.

Often at work, when working on the codebase, I need to check out different branches. The most common scenario is when I want to check out a branch to review a pull request. Other times, I might want to create different branches to try things out and compare the code later on.

Before I started using git worktree, I always found it a hassle to switch branches midway while working on some code. I would either have to make a commit or stash the changes before switching to another branch. Comparing the code of two branches in my code editor at the same time was also not straightforward.

Creating git worktrees

Using git worktree, besides the main worktree we can have additional worktrees (“work directories”) linked to the same repository. Note that if you create a bare repository, it starts with zero worktrees linked. With git worktree you can easily check out multiple branches, each with its own working directory. Checking out a branch is simply a matter of changing directories.

To create a new worktree, the following command can be used inside a (bare) repository:

git worktree add <path> -b <branch>

The -b <branch> part is optional. For example:

git worktree add my_example

will create a new working directory my_example with branch my_example.

Organizing git worktrees

For each project, I like to create a root folder where I will add worktrees. To keep things clean, I make use of a hidden .bare repository that .git will be linked to:

# create a folder to host worktrees 
mkdir project_root/
cd project_root
# clone bare repo inside .bare to make it hidden
git clone --bare <repo_url> .bare/
# link folder root to bare git repo
echo "gitdir: ./.bare" > .git

This results in the following folder structure:

project_root/
  |__ .bare
  |__ .git (links to .bare repo)

To create a new worktree, for example one that links to the main branch, I can do the following inside the project_root folder:

git worktree add project_main main

This will create a folder project_main linking to the main branch:

project_root/
  |__ .bare
  |__ .git (links to .bare repo)
  |__ project_main (links to main branch)

Using git worktree add you can now add as many worktrees as you like:

project_root/
  |__ .bare
  |__ .git (links to .bare repo)
  |__ project_main (links to main branch)
  |__ feature_branch1 (links to feature_branch1)
  |__ feature_branch2 (links to feature_branch2)
  |__ ...

Managing worktrees

Check out the official git worktree documentation for an overview of the available commands to work with worktrees. Some useful ones that you will use more often are:

You might want to smooth out your git worktree workflow by creating some bash scripts to automate things like cloning and setting up the folder structure, using fzf to check out branches in new worktrees, cleaning up old worktrees, etc.


 

Previous Post
Making API Calls From The Terminal
Next Post
From AI To Language Models