Git worktrees for parallel AI coding agents: what they fix and what they don't
Run several AI coding agents on one repo with git worktrees: the commands, the traps (ports, .env, shared hooks, one database), and why worktrees still end in merge conflicts.
You want three agents working at once: one on the checkout page, one on a bug, one writing tests. If they share one working copy, they overwrite each other’s files mid-task and nobody can tell whose change broke what.
The standard answer is git worktrees: one checkout per agent, all backed by the same repository. This guide sets that up, covers the traps people hit in the first hour, and then the part worktrees don’t solve: the agents still meet at merge time.
One worktree per agent
A worktree is a second (third, fourth) checkout of the same repo in its own
directory, on its own branch. Commits made in any of them land in the same
.git, so there is nothing to clone or sync.
From your main checkout:
git worktree add -b agent/claude/checkout ../app-checkout main
git worktree add -b agent/codex/fix-1234 ../app-fix-1234 main
git worktree add -b agent/cursor/tests ../app-tests main
git worktree list
Start each agent in its own directory (cd ../app-checkout first), and tell
it which branch it owns. Branch names that say which agent and which task, like
agent/claude/checkout, cost nothing and make every later question easier:
who changed this, whose branch conflicts with whose.
When a task is merged:
git worktree remove ../app-checkout
git branch -d agent/claude/checkout
If a worktree directory was deleted by hand, git worktree prune clears the
stale entry.
Some agent tools now create worktrees for you. The traps below apply either way.
The traps in the first hour
One branch, one worktree. Git refuses to check out a branch that is
already checked out in another worktree. Agents never work on main itself;
each gets a fresh branch.
Dependencies are per worktree. node_modules, a Python virtualenv or a
build directory are not shared, so each worktree needs its own install. Budget
the disk space, and put the install step in the agent’s instructions.
Untracked files don’t come along. Your .env is untracked, so a new
worktree doesn’t have it. The agent will notice the missing variables and
either stop or invent a fix. Copy what it needs on purpose, and check that
.gitignore still covers it there. An agent that recreates .env from memory
with a real key in it is how keys end up in commits.
Ports and databases clash. Three agents running npm run dev all want
port 3000, and three test suites against one local database delete each
other’s rows. Give each worktree its own port (PORT=3001 in its environment)
and, if the tests write to a database, its own database name.
Hooks and config are shared. Git hooks live in the common .git
directory, so every worktree runs the same pre-commit hook, and a change to
.git/config in one worktree changes it for all of them. That is usually what
you want. It also means an agent that edits or skips a hook in its worktree
has done it for everyone.
What worktrees don’t prevent
Worktrees isolate the working copies. They don’t isolate the files the branches will be merged into. Agents on related tasks drift to the same places: the changelog every task adds a line to, the route table every feature registers in, the shared config. Each branch is fine on its own; the conflict appears when the second one is merged, after both agents are done.
In a small simulation on a copy of this repo,
one append-only CHANGELOG.md blocked three of four agents’ merges by itself.
A few habits remove most of it:
- Let append-only files merge themselves.
CHANGELOG.md merge=unionin.gitattributeskeeps both sides instead of conflicting. Only for files where line order doesn’t matter. - Split work by files, not only by task. If two tasks will touch the same module, run them one after the other. The cheapest conflict is the one that never starts.
- Merge small and often. Rebase each branch on
mainafter every merge, so the next conflict is a small one. - Test the merged result, not just each branch. Two branches can each pass their tests and break together. Run the suite on the combined result before the second merge, not after.
See the overlap while the agents still work
All of that happens at merge time. It is earlier and cheaper to know that two agents are editing the same file while they are still working.
Have each agent push its branch after every working step, not only at the end:
Commit and push your branch after each working step, not only at the end.
Then something that sees every branch can compare them. That is what the Overlaps page in nimblegate does: the agents push to a self-hosted gateway instead of straight to GitHub, and it lists pairs of open branches that change the same files, now and on each push, with an optional webhook so whatever runs your agents hears about it. The overlap alerts guide has the setup, and the live demo shows the page. Because every push goes through the same gateway, the same pass also checks what the agents push: keys, force-pushes, skipped tests. Unlike the shared hook, an agent can’t edit that check from its worktree.
Checklist
- One worktree and one fresh branch per agent:
git worktree add -b agent/<tool>/<task> ../app-<task> main. - Install dependencies in each worktree; copy only the untracked files it needs.
- A port and a database name per worktree.
merge=unionfor append-only files; split tasks by files.- Agents push after every step; watch for branches that touch the same files.
- Merge one branch at a time, rebase the rest, test the combined result.
git worktree removeand delete the branch when it’s merged.
← All posts · nimblegate is a self-hosted push gateway for AI agents - how it works · live demo