You are running Claude Code on a refactor. While it works, you want Aider on a test fix and a Cursor Background Agent on a dep bump. All three are about to land on `main`, and you have a half-finished session that cannot safely close.
The fix is a five-minute setup: **git worktrees**. One branch per agent, isolated working directories sharing one `.git` database. No destructive `checkout`, no shared-state races.
A coding agent wants exclusive access to a working tree — it reads, writes diffs, runs tests. Launch a second agent on the same checkout and they race on the same files, both try to run the test suite, and you spend the next hour cleaning up nonsense diffs.
The default workaround is branches. The problem: switching mid-session is destructive. In-flight agent state — open files, partial edits, unsaved work — gets clobbered the moment you `git checkout` away from the branch the agent is working on.
Worktrees solve this. Each is a separate working directory bound to its own branch, all sharing one `.git` database. Five worktrees, five agents, no branch switching.
Nothing to install. Worktrees ship with git.
git config --global worktree.guessRemote true
cd ~/projects/myapp
git worktree add ../myapp-agent-a -b feat/api-refactor
git worktree add ../myapp-agent-b -b fix/test-flakes
git worktree add ../myapp-agent-c -b chore/bump-deps
Three directories, three branches, one shared `.git` database.
Open each worktree in its own terminal tab, IDE window, or tmux pane. Launch a different agent in each:
cd ~/projects/myapp-agent-a
claude "Refactor the auth module to use the new token format"
cd ~/projects/myapp-agent-b
aider src/tests/ --model claude-sonnet-4-5
Each agent reads, writes, and tests in its own directory. The shared `.git` database keeps the branches aware of each other, but the working trees never collide. When agent A finishes, you merge from the main checkout. When two branches conflict, git tells you where.
Yesterday I ran three agents on the same monorepo in parallel:
Total wall-clock: 12 minutes. Sequential would have been 24+ minutes, plus re-running agent A's tests after agent C's `npm audit` rewrote the lockfile. The merge had one conflict — in `requirements.txt`, where the refactor and the dep bump both touched the same version specifier. Resolved in 30 seconds.
**Each worktree must be on its own branch.** Two worktrees on the same branch will corrupt git's state.
**Your IDE may not auto-detect new worktrees.** VS Code's "Open Folder" works fine; some JetBrains versions need a restart.
**Disk space doubles per worktree.** A 2GB monorepo times three is 6GB.
**Cleanup matters.** `git worktree remove ../myapp-agent-a` deletes the directory and the branch reference. `git worktree list` shows what is mounted. Forgotten worktrees pile up fast.
Git worktrees are the most underused tool in the multi-agent workflow. Five minutes of setup, one config command, and the payoff is the difference between running agents in series and running them in parallel. Do it before your next refactor.