Run parallel AI coding agents with git worktrees
A coding agent spends most of its time waiting: on the model, on the test suite, on you. The obvious fix is to run two or three at once. The obvious way to do that, opening another terminal in the same folder, breaks within minutes. Both agents edit the same files, one runs git checkout under the other, and the test failures you see belong to whichever agent saved last.
Git worktrees solve the file problem. Each agent gets its own folder and its own branch, backed by the same repository. This post covers the setup, the parts worktrees don't isolate (ports, databases, env files), and how to split work so the branches still merge.
What a worktree is
A worktree is an extra checkout of the same repository in another directory. The git manual puts it as "sharing everything except per-worktree files such as HEAD, index, etc." In practice:
| Shared across worktrees | Separate per worktree | |
|---|---|---|
| Commits and objects | ✓ | — |
Branches and tags (refs/) |
✓ | — |
| Repository config | ✓ | — |
Checked-out branch (HEAD) |
— | ✓ |
| Staging area (index) | — | ✓ |
Files on disk, including node_modules and .env |
— | ✓ |
A commit made in one worktree is visible from every other one at once, with no push or fetch. That's what makes them cheaper than cloning the repo three times.
Set one up by hand
From the main checkout, create a worktree next to it on a new branch, then start the agent inside it:
git worktree add ../myapp-auth -b feat/auth
cd ../myapp-auth
claude
Repeat with a different path and branch for each parallel task. To see what exists and remove one when its branch is merged:
git worktree list
git worktree remove ../myapp-auth
Three things that trip people up:
- A branch can be checked out in only one worktree.
git worktree add ../other feat/authfails with'feat/auth' is already used by worktree at .... That rule is a feature: it stops two agents from committing to the same branch. removerefuses a worktree with uncommitted or untracked files. It printscontains modified or untracked files, use --force to delete it. Read what's there before you add--force, because the agent's unfinished work is exactly what gets deleted.- Removing the worktree keeps the branch. Its commits are safe; delete the branch separately with
git branch -donce it's merged.
If you delete a worktree folder with rm or Explorer instead, run git worktree prune to clear git's record of it.
Or let Claude Code create them
Claude Code has this built in. From the repository root:
claude --worktree feature-auth
-w is the short form. Per the Claude Code worktree docs, this creates the checkout under .claude/worktrees/feature-auth/ on a new branch named worktree-feature-auth, branched from the default branch. Run it again with another name in a second terminal for a second isolated session. Leave the name out and Claude picks a random one.
When you exit, Claude Code removes a worktree with no changes on its own; for a named session it asks first. If the worktree has work in it, you choose whether to keep it, and it prints a claude --worktree <name> --resume command to pick up later. Choosing remove deletes the directory, the branch and the work in them, so commit or keep anything you want first. Worktrees started by non-interactive claude -p runs are never cleaned up automatically; remove them with git worktree remove.
Add the folder to .gitignore so the checkouts don't show up as untracked files in your main checkout:
.claude/worktrees/
You can also make isolation the default for a custom subagent. Add isolation: worktree to its frontmatter in .claude/agents/, and each run gets a temporary worktree that's removed if the subagent made no changes. That suits mechanical jobs, like a rename across many files, that you want to review as one branch.
Copy the gitignored files each worktree needs
A new worktree contains only tracked files. Your .env, local config and installed dependencies aren't there, so the agent's first test run fails and it may start "fixing" code that was never broken.
Claude Code reads a .worktreeinclude file in the project root, in .gitignore syntax, and copies matching files into every worktree it creates. Only files that match a pattern and are also gitignored get copied:
.env
.env.local
config/secrets.json
For worktrees you make by hand, copy the files yourself, then install dependencies:
cp .env ../myapp-auth/
cd ../myapp-auth
pnpm install
pnpm hard-links packages from one global store, so a second or third install is fast and takes little extra disk. With npm, each worktree gets a full node_modules.
Copying .env puts the same secrets in more places. If you block the agent from reading secrets with a deny rule, that rule still applies in the worktree; see Vibe coding security checklist for the setting.
Isolate what worktrees don't
Worktrees separate files. Everything outside the folder is still shared, and that's where most parallel-agent bugs come from:
- Ports. Two dev servers can't both listen on 3000. Give each worktree its own port in its
.env(PORT=3001,PORT=3002) or pass it on the command line (npm run dev -- --port 5174for Vite). Tell the agent which port is its own, or it may test against the other agent's server and report success. - Databases. If every worktree points at the same local database, one agent's migration breaks the other agent's tests. Use a database per worktree (
DATABASE_URL=postgres://localhost/myapp_auth) or a disposable container per task. - Caches and build output outside the repo. Anything written to a fixed path in your home directory or
/tmpis shared. Most tools keep caches inside the project, but check before you trust two parallel test runs. - Your attention. Three agents that each need a decision every few minutes will cost you more than they save. Two parallel tasks plus one you're actively steering is a sensible starting point.
Split the work so it merges
Worktrees make conflicts visible at merge time instead of preventing them. Pick tasks that touch different parts of the code:
- Good splits: a new API endpoint and an unrelated UI page; a bug fix and a test-coverage task in another module; a dependency upgrade and documentation.
- Bad splits: two features that both edit the same router, schema or shared component. Run those one after the other.
- Shared foundations first. If two tasks need the same new type or database column, land that change on the main branch, then start both worktrees from it.
Write each task down before you start the agent: the goal, the files it may touch, and how to check it's done. The same instructions that keep a single agent on track apply here; How to write an AGENTS.md or CLAUDE.md that coding agents follow covers that file. Your AGENTS.md or CLAUDE.md is tracked, so every worktree gets the same copy.
Review and merge one branch at a time
When an agent says it's done, review its branch like a pull request from a colleague:
git log --oneline main..feat/auth
git diff main...feat/auth
The three-dot diff shows only what changed on the branch since it split from main. Merge the first finished branch, then rebase the others onto the new main before reviewing them, so each review shows that agent's changes only:
cd ../myapp-billing
git rebase main
If the rebase conflicts, the agent that owns the worktree is usually the fastest one to resolve it, because it knows what its change was meant to do. Run the full test suite after each merge, not only once at the end.
Keep spend per task visible
Parallel agents multiply token use, and a runaway loop in a terminal you aren't watching can burn through a budget unnoticed. If you run Claude Code through ChromaLab, give each parallel session its own API key with a daily token budget, set in that terminal:
export ANTHROPIC_BASE_URL="https://chromalab.ink"
export ANTHROPIC_AUTH_TOKEN="chroma-key-for-this-task"
claude --worktree feature-auth
Usage then shows per task, and a session that hits its budget stops with token_budget_exceeded instead of draining the plan. Budgets reset at UTC midnight. The settings are described under per-key controls, and the base setup is in Run Claude Code on any model through ChromaLab.
Checklist
- One worktree and one branch per agent; never two agents in one folder.
.claude/worktrees/in.gitignoreif you useclaude --worktree..envand other gitignored essentials copied in, through.worktreeincludeor by hand, and dependencies installed.- A separate port and database for each worktree, written in its
.envand told to the agent. - Tasks chosen so they touch different files; shared foundations merged first.
- Each branch reviewed with
git diff main...branch, merged one at a time, the rest rebased. - Finished worktrees removed with
git worktree remove, merged branches deleted.