Claude Code Multiple Agents in Git Worktrees: Our Setup
Running multiple Claude Code agents in git worktrees is the simplest way we know to let agents work in parallel without overwriting each other. Each agent gets its own folder and branch from one repository. This article covers the setup we use, the commands, and the collisions that still happen. It is one part of how we build.
What a worktree is
A git worktree is another checkout of the same repository, in a separate folder, on its own branch. It shares the object store with the main checkout, so it is lighter than a second clone. Git refuses to check out the same branch in two worktrees at once, and that refusal is useful: it enforces one owner per branch.
git worktree add ../app-export-empty-state -b feat/export-empty-state
cd ../app-export-empty-state
The setup, step by step
- One worktree and one branch per builder, named for the task. Never share a worktree between two agents.
- Install dependencies in each worktree. With pnpm, the shared package store makes this cheap.
- Copy what git ignores. A new worktree has no
.envfiles or other untracked local setup. Copy what the task needs, and no secrets it does not. - Give each agent its brief and its folder only. The brief names the area it may change. The orchestrator pattern describes what a brief contains.
- Name shared files in every brief. Lockfiles, configuration, generated files and schema files are the usual collision points. Only one builder touches each at a time.
- Land through one place. Builders never push. Finished changes merge into one shared integration branch, and one agent ships it to CI. A builder whose change conflicts rebases onto that branch and lands again.
- Remove the worktree once the change merges. Run
git worktree remove <path>, then delete the branch.git worktree listshows what is still around, andgit worktree pruneclears records of folders already deleted.
Where to put them, and how to keep track
Create worktrees as sibling folders of the repository, not inside it. Editors, linters and test runners that scan the repository folder otherwise find a second copy of the project and index it twice.
Start each planning session with git worktree list and compare it with the open pull requests. A worktree with no pull request and no recent commits is either finished or abandoned. Ask its owner before you remove it, and never delete one that still has uncommitted changes until you know they are not needed.
Claude Code’s own worktree option
Claude Code subagents can set isolation: worktree in their frontmatter, which runs the subagent in a temporary worktree. We checked Anthropic’s documentation on 3 December 2026. By default the copy branches from your default branch, not from the parent session’s current HEAD. If your work builds on a feature branch, check that the base is the one you expect before you trust the result. This is a feature that changes, so read the current docs.
Collisions that still happen
Two agents edit one file. Worktrees do not prevent this; they delay the conflict to merge time. The fix is the brief: a separate area for each agent, shared files named, and the later change rebasing onto the earlier.
Lockfiles. Two agents that add dependencies produce a conflicting lockfile. Let one builder own dependency changes, or regenerate the lockfile once after merging.
Ports. Two dev servers on the same port fail in confusing ways. Give each preview its own port, and write it in the brief.
Memory. Every dev server uses RAM. One preview of our largest app can take up to 2.5 GB even with its heap capped, on a 16 GB laptop, so we start one only when a script confirms there is room left, and we cap the number of builders at four.
Broad kill commands. pkill node stops every agent’s server, not only yours. Kill by process ID or port.
Stale worktrees. After a few days there are dozens of folders and branches nobody owns. Clean up when a change merges, and list the worktrees at the start of each planning session.
Is it worth it
For one agent on one task, a worktree is extra ceremony; a normal branch is enough. It earns its keep from the second agent onward. The cost is a little setup per task and the discipline to clean up. The benefit is that no agent ever sees a half-finished edit from another.
Questions
Why use git worktrees for multiple Claude Code agents?
A worktree gives each agent its own folder and branch from one repository, so two agents never edit the same files in the same checkout. Without it, agents overwrite each other's uncommitted work. Worktrees also share one object store, so they are cheaper than separate clones.
How many Claude Code agents can run in parallel?
We run at most four builders at once. The limits are the memory of the machine, since each agent may start a dev server, and your own capacity to try and review what comes back. Past that point, extra agents mostly create a queue of unreviewed changes.
Do git worktrees stop merge conflicts between agents?
No. They move conflicts from the working folder to merge time, which is easier to handle but still real. You avoid most of them by giving each agent its own area and naming shared files, such as lockfiles and configuration, in every brief.