Contents

Worktree - a new git tree

The worktree mechanism in git

Many developers use git for version control. It has become something of a standard in the modern developer’s toolbox. When we start a new task, we begin by creating a branch for our changes. But what if we need to switch between tasks and changing branches is just not convenient enough? That’s where worktree comes to the rescue.

Warning
This article was translated with Claude from polish language. I have made every effort to make it correct. However, if you found any errors or inaccuracies, please let me know.

tl;dr

  • git worktree lets you work on several branches at once, each in a separate folder
  • All folders share a single repository, so commits made in one are immediately visible in the others
  • You create a new worktree with git worktree add, list them with git worktree list, and remove them with git worktree remove
  • A branch can be checked out in only one worktree at a time
  • Git configuration and hooks are shared, and private files can be copied automatically with a post-checkout hook
  • It comes in handy for long-running tests, separate builds and working in parallel with AI agents

What is a worktree?

The situation is simple. We have our project folder and want to start working on a new task. So we create a new branch and get to work. We launch the application, and when we’re already knee-deep in the work, our manager asks us to check something in the meantime.

Usually in such a case we commit our changes (or use git stash) so as not to lose our work, and then switch to a new branch in the same folder. It’s not a problem when all it costs us is a branch switch, but if we already have the project compiled in a specific version there, getting back to the previous state can be quite nerve-racking.

It may also happen that we want to run some tests on the project and fix things as we go. If they take a long time and run in the background, they can block our work – unless we make a copy of the whole folder.

New task, new folder

The smarter ones will create a new folder and start making their new changes there. It’s a convenient solution, because it keeps the working folders apart and separates the packages generated during the build from the rest of the work. Unfortunately, such a copy is in practice a separate repository – commits made in one folder aren’t visible in the other without adding a remote and running git fetch, which makes merging between branches harder.

This is where worktree comes in. It lets you create a new folder with a separate working branch, but keeps the connection within the version control system. So we can compare our changes between the created branches without linking repositories through additional remotes.

Additionally, if we delete a folder created with worktree, the things done on that branch remain in our history (as long as we commit them).

The AI angle

It’s also worth mentioning that this feature is useful when working with AI agents. We can have them work in a loop on a dedicated branch, giving them an independent copy of the program. If we need to run some tests or launch the program, it won’t collide with the other environment where we’re working on something else. It’s a good idea to run the program in a container here, so that the machine’s ports don’t collide either, but that’s a topic for another post.

Interestingly, agents such as Claude Code can create their own worktrees so as not to modify our code. From that point of view, it’s nothing new to them.

Using git

As I mentioned earlier, we’ll use git worktree to create a copy of our project in another folder with a different branch.

New worktree

Let’s assume we’re creating a directory named project-feature1 and a new branch for it, branch/feature-1.

git worktree add -b branch/feature-1 ../project-feature1

We can also work on a previously created branch:

git worktree add ../project-feature1 branch/feature-1
Warning
Keep in mind that, for safety and data consistency reasons, git won’t let you check out a branch that is already in use in another worktree. We can’t have copies of the same branch in two places. We need to create a new branch.

Listing worktrees

If we want to check which worktrees exist and which branches they’re on, we just type:

git worktree list

Removing a worktree

Once we’ve finished working on our task and no longer need the folder, we can remove the worktree.

git worktree remove ../project-feature1

It may also happen that we accidentally (or deliberately) delete the folder with our feature. In that case the prune command comes to the rescue – it cleans up git’s metadata and brings it in line with the actual state.

git worktree prune
Info
Note that even though our folder has been deleted, the committed changes still remain in the branch. That wouldn’t be the case if the folder with the changes had been created by simply copying it.

Shared configuration

An undeniable advantage of git worktree is that the git configuration is shared between folders. All of the project’s git settings are therefore visible in every worktree. This applies, for example, to .git/info/exclude, which holds the list of files excluded from version control.

Tip

By default these settings are shared. If we need settings for just one worktree, we can enable the extensions.worktreeConfig option and then save them with git config --worktree. They then go into the .git/worktrees/<name>/config.worktree file and don’t affect the other folders. This doesn’t apply to info/exclude, though, which is always shared.

git config extensions.worktreeConfig true
git config --worktree core.sparseCheckout true

However, it may happen that we use a private configuration during our work that isn’t part of the repository (e.g. we have our own AI configuration files). git worktree add only creates files tracked by git, so in that case we have to copy such files (e.g. .env or local configs) into the newly created folders ourselves.

Fortunately, this process is easy to automate with git hooks. The hook called post-checkout is helpful here. It’s invoked every time after a successful checkout.

#!/usr/bin/env bash

PREV_HEAD=$1
NEW_HEAD=$2
CHECKOUT_TYPE=$3

# Run only on `git worktree add` (null previous HEAD, branch checkout)
if [[ ! "$PREV_HEAD" =~ ^0+$ ]] || [[ "$CHECKOUT_TYPE" != "1" ]]; then
  exit 0
fi

# My code after `git worktree add`

If, for example, we use a stow configuration (as here), we can try something like this:

#!/usr/bin/env bash

PREV_HEAD=$1
NEW_HEAD=$2
CHECKOUT_TYPE=$3

# Run only on `git worktree add` (null previous HEAD, branch checkout)
if [[ ! "$PREV_HEAD" =~ ^0+$ ]] || [[ "$CHECKOUT_TYPE" != "1" ]]; then
  exit 0
fi

WORKTREE_PATH=$(pwd)

cd ~/configuration-folder
stow --target "${WORKTREE_PATH}" configuration_variant

We save the file in the main repository as .git/hooks/post-checkout – hooks are shared by all worktrees. Don’t forget to make it executable with:

chmod +x .git/hooks/post-checkout

Why would I need this

And now a few situations where git worktree can come in handy:

  • Separating tasks that produce temporary reports not subject to version control
  • Building the application for different environment versions without overwriting the build
  • Longer tests that don’t block the main code branch
  • Working with agents on several tasks at once, especially when we give them longer jobs
  • Giving code presentations when we don’t want to overwrite our settings in the middle of work

Summary

As we can see, git worktree is an interesting way to separate work on different tasks in the code. Not only does it let us create separate, temporary working environments, but they all share a common history and branches – a commit made in one folder is immediately visible in the others. This makes it easy to browse the history of the whole repository from any of the branches.

It’s therefore a perfect fit for working with AI and with many tasks at the same time. It’s also worth combining it with other technologies, such as containerization or tmux for separating terminals. I think it’s one of those features that will prove useful in a developer’s ever faster-changing work.

References

  1. https://git-scm.com/docs/git-worktree - Official git documentation describing the command