4 min readAi
Git Worktrees: Run Several AI Agents at Once Without the Mess
A Git worktree is an extra folder for the same repo, on its own branch. Give each Claude Code agent its own worktree and they can all work at once without breaking each other.
- #git
- #ai
- #claude-code
- #agents
- #developer-tools
Git Worktrees: Run Several AI Agents at Once Without the Mess
AI agents like Claude Code can work on their own for a long time. So why not run two or three at once? Because if they share one folder, they overwrite each other's work. Git's fix for that is worktrees.
What is a worktree?
A normal repo is one folder with one branch checked out. To work on something else, you stash and switch, and your files change in place.
A worktree is an extra folder for the same repo. Each one has its own branch and its own files, but they all share one history (one .git).
shop/ ← on main
shop-login/ ← on feat/login
shop-cart/ ← on feat/cart
Edit a file in shop-login/, and shop-cart/ doesn't change. Commit in shop-cart/, and the commit shows up everywhere.
How it helps
- No stash and switch. Each task has its own folder. Just
cdto it. - Quick side jobs. Review a PR or fix a bug without touching your half-done work.
- Parallel AI agents. One agent per worktree, each on its own branch.
§ Try it — Git worktrees
Shared .git (one history)
- main1 commit
- feat/login1 commit
Folders on disk
shop/[feat/login]
Agent A
login.js
One folder, one branch, one agent. All good.
The commands
Create a worktree next to your repo, on a new branch:
git worktree add ../shop-login -b feat/login
See them all:
git worktree list
/code/shop 3ca04eb [main]
/code/shop-login 3ca04eb [feat/login]
Work in it like any folder. cd in, edit, commit, push:
cd ../shop-login
git add .
git commit -m "feat: add login"
git push -u origin feat/login
Delete it when you're done:
git worktree remove ../shop-login
One rule: a branch can only be in one worktree at a time. Git stops you if you try twice. That's what keeps two agents off the same branch.
With Claude Code
Claude Code does all of this for you with one flag:
claude --worktree login
It makes a worktree at .claude/worktrees/login/ on a new branch worktree-login, and starts the session there. Open another terminal for the next task:
claude --worktree cart
Two agents, two folders, two branches. When you exit, Claude Code removes the worktree if nothing changed, or asks you what to do if there's work in it.
Add this to .gitignore so the folders don't show up as untracked files:
.claude/worktrees/
Subagents can get their own worktree too. Ask Claude to "use worktrees for your agents", or add isolation: worktree to a custom agent's front matter.
Watch out for
- It's a fresh checkout.
node_modulesand.envaren't copied. Runnpm installin each one. With Claude Code, list files like.envin a.worktreeincludefile and they're copied for you. - Ports clash. Two dev servers can't both use port 3000.
- Separate tasks merge cleanly. Two agents editing the same files will still give you merge conflicts at the end.
That's it
A worktree is just another folder for the same repo, on its own branch. Give each agent its own one, and they can all work at the same time without getting in each other's way.
More in the git worktree docs and the Claude Code worktrees guide.