AI coding agents get weird the moment you run more than one in the same checkout. One session rewrites a file, another stages something half-finished, and a third grabs .git/index.lock right when you want to run tests. The result is not parallelism. It is shared-state chaos.
Git worktrees are the cleanest fix I know for this. They let each agent keep its own working directory and index while still sharing the same repository history. That means Claude Code can work on a refactor, Cursor can patch a test failure, and Codex can draft docs, all without fighting over one folder.
This post covers a practical worktree workflow for multi-agent development that stays fast, reviewable, and boring in the good way.
Why one checkout breaks down so quickly
A single AI agent in a repo is manageable. Multiple agents in the same directory create several failure modes immediately:
- one agent edits files another agent assumed were stable
- staged changes leak across tasks
- shell commands run against the wrong branch state
.git/index.lockcollisions show up at the worst moment- local ports, test databases, and caches get shared accidentally
Humans notice some of that. Agents often do not. They keep going with stale assumptions until the diff becomes painful to review.
What Git worktrees actually give you
A Git worktree is a linked working directory attached to the same repository. Each worktree has its own checked out files, HEAD, and staging area, while Git history and objects stay shared.
That balance is exactly what multi-agent workflows need:
- separate filesystems per task so edits do not collide in place
- separate indexes so staging stays scoped to the task
- shared object database so you are not recloning the repo repeatedly
- cheap creation and cleanup compared with full clones
In practice, that means you get isolation where it matters for coding, without paying the cost of duplicating the whole repository for every agent.
The branch-per-agent pattern I recommend
The simplest durable pattern is one branch and one worktree per agent task.
For example:
git worktree add ../wt-auth-hardening -b ai/auth-hardening origin/master
git worktree add ../wt-api-tests -b ai/api-tests origin/master
git worktree add ../wt-docs-cleanup -b ai/docs-cleanup origin/masterThen each agent session starts inside exactly one worktree:
../wt-auth-hardeningfor the auth task../wt-api-testsfor test fixes../wt-docs-cleanupfor docs and polish
This sounds obvious, but the naming matters. If the folder name, branch name, and task title all line up, review stays much easier later.
Good worktree names save real time
A naming convention does more work than people expect. I like:
wt-<task>
ai/<task>Examples:
wt-checkout-bug+ai/checkout-bugwt-refactor-billing-api+ai/refactor-billing-apiwt-add-login-tests+ai/add-login-tests
Worktrees solve file conflicts, not runtime conflicts
This is the part teams miss. Worktrees isolate the repository state, but they do not automatically isolate everything around the repository.
If two agents both run npm run dev on port 3000, one of them is still going to lose. If they both point at the same mutable test database, they can still corrupt each other’s assumptions.
For parallel AI work, add a few runtime rules next to worktrees:
- assign unique ports per worktree
- use separate
.envoverlays when local config differs - isolate test databases or schemas when tests mutate data
- scope caches and temp directories where tools allow it
- avoid shared local state that outlives the branch
A useful mental model is this: worktrees isolate code state, not service state.
A lightweight parallel setup that works well
For most repos, you do not need a huge orchestration layer. You just need predictable setup.
# create task worktrees from a clean main checkout
git fetch origin
git worktree add ../wt-bugfix-cart -b ai/bugfix-cart origin/master
git worktree add ../wt-upgrade-eslint -b ai/upgrade-eslint origin/master
# inspect active worktrees
git worktree listThen inside each worktree, keep the session narrow:
cd ../wt-bugfix-cart
pnpm install
pnpm test tests/cart.test.tsA second agent can work elsewhere without touching that state:
cd ../wt-upgrade-eslint
pnpm install
pnpm lintWhere AI agents benefit the most
Worktrees are especially useful when tasks are independent but still live in the same repo.
1. Parallel bugfixing
One agent can handle a production bug while another updates tests or cleans up docs. No stashing, no branch thrash, no wondering which files are safe to touch.
2. Large refactors broken into reviewable chunks
Instead of asking one agent to perform a giant rewrite, split the work into separate branches and review them independently.
3. Comparison runs across tools
You can give the same repo state to different tools, like Claude Code, Cursor, and Codex, in separate worktrees and compare outputs cleanly.
4. Safer experimentation
Detached or throwaway worktrees are handy when you want an agent to explore a risky path without contaminating a real task branch.
A merge discipline that keeps the benefits
Parallel agent output only helps if you merge it carefully. My default rules are:
- each worktree produces one task-scoped PR
- each PR verifies against its own branch before merge
- merge the lowest-risk or highest-dependency PR first
- rebase or regenerate later worktrees when upstream assumptions change
If PR A changes a core interface, PR B and PR C should not quietly pretend nothing happened. Re-sync the dependent worktrees and rerun checks.
Common mistakes that make worktrees feel worse than they are
Reusing one branch across multiple worktrees
That defeats the point. One task should have one branch owner.
Forgetting cleanup
When a task is merged or abandoned, remove the worktree properly:
git worktree remove ../wt-bugfix-cart
git branch -d ai/bugfix-cartIf directories disappear manually, run git worktree prune from the main repo to clean stale metadata.
Sharing mutable local infrastructure
If all worktrees hit the same ports, Redis instance, or writable scratch space, you still get hidden cross-talk.
Letting agent tasks overlap too much
Worktrees help when tasks are separable. If three agents are all rewriting the same core service, you have a coordination problem, not a Git problem.
A small playbook you can actually adopt
- keep
masterormainclean and up to date - create one worktree per agent task from that clean base
- name the directory and branch after the task
- give each worktree its own verification commands
- isolate runtime state when services or tests are mutable
- merge in dependency order, not arrival order
- clean up worktrees immediately after merge
It is not fancy, but it scales much better than one shared checkout with multiple chat sessions pointed at it.
References and resources
- Git worktree documentation
- Git worktrees for parallel AI coding agents
- Parallel AI agent execution with worktrees
Takeaways
- Use one branch and one worktree per AI task
- Treat runtime isolation separately from repository isolation
- Keep branch names, folder names, and PR scope aligned
- Clean up merged worktrees so the system stays easy to reason about