Back in March, a colleague of mine, the same one as before, came to me and said:
Have you figured out how to run parallel agents with Claude Code?
I answered with the negative.
He continued:
Do you know worktrees?
Again, I didn’t know about this feature of the popular versioning control software. So he presented the feature, native to Git, and then it clicked in my mind!
What Are WorkTrees
Git worktrees let you check out multiple branches of the same repository simultaneously, each in its own directory, sharing a single .git database. Instead of stashing or committing half-finished work to switch branches, you just cd into another worktree.
To get started, you need to clone the repository as bare:
|
|
Then, you can browse the repository’s main folder and can create your first worktree.
However, bare clones, by default, don’t create remote-tracking branches (refs/remotes/origin/*), like the main that you’ll probably need. Instead, the remote branches are mapped directly to local refs. You can verify with git branch -a—you won’t see origin/main.
To fix that, fetch with the standard refspec first:
|
|
Now origin/main exists and the following worktree command will work:
|
|
Why this happens: git clone --bare sets the fetch refspec to +refs/heads/*:refs/heads/*, mirroring remote branches as local branches rather than tracking branches. The config change above restores the usual non-bare behavior.
Next you can create a new worktree:
|
|
The above command creates a new working directory feature-branch linked to the new branch feature-branch.
Running git worktree list will show all active worktrees, and git worktree remove cleans one up.
The main constraint is that no two worktrees can have the same branch checked out at the same time. So if you used to use git checkout to switch from a branch to another, you’ll need to stop.
Once you’ve made your feature changes, you simply push the branch to remote and create the PR.
After the PR merge, you can clean up the local repository with several commands, some you know already, some new because of the worktree usage:
|
|
You can read more about them in the official docs.
Why Worktrees For Agent Parallel Runs
It may seem obvious to some of you, but the ones who didn’t get yet, I’ll explain now.
Using worktrees with Claude Code will enable you to work in parallel on two different features.
You simply launch two terminals, each with its Claude Code running and you can tackle the development of a feature.
It’s similar to have two developers working on the same project, but different features. And with that comes the same issues: if both feature touch the same files, you’ll run into merge conflicts when merging the second feature after the first was integrated in the main branch of the repository.
Caveats With Worktrees
There’s one and it has to do with Windows and a long path. When I tried the agent workflow with worktrees, I had a worktree name like this: fix_the-back-button-in-browser-fail-to-load-index-page-from-a-platform-page.
Then, when the code reviewer and test runner agent came onto the scene, they both failed. Why? Because the full path was /e/Git/GitHub/SocialMediaPublisherApp.git/fix_the-back-button-in-browser-fail-to-load-index-page-from-a-platform-page/.
The worktree folder’s name is extremely long. When the agent tries to cd into it via bash, MINGW64 on Windows can choke on very long paths, causing commands to silently fail or error out before even running npm commands.
I capped at 30 chars the slug evaluation and the issue never came back.
But, if you are on Windows, use WSL to emulate a Linux OS on Windows without its limitations.
Going Beyond Worktrees
Now that you know what are worktrees and how you could use them in your workflow, you can start using Worktrunk, a Git worktree management tool for parallel AI agent workflows. It provides a better DX to you and Claude.
To be honest, I am not using parallel Claude Code a lot. I don’t spend enough time (or maybe tokens 🙃) to really need it, but I have integrated it in my team of agents. You can look at this repository as an example to review my agents.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.