Track lessons
Run Multiple AI Agents in Parallel with git worktree
AI coding sessions often edit many files at once. Running two agents in one checkout makes them overwrite changes, switch branches underneath each other, and confuse uncommitted work; worktrees give each session an isolated folder while sharing the same Git history.
When to use
- Run independent agent tasks at the same time without cloning the repository twice.
- Keep a long-running feature open while an agent handles an urgent fix.
- Review one agent's branch while another agent continues working.
Example
An auth agent works in ../myapp-auth on agent/auth while a billing agent works in ../myapp-billing on agent/billing. They share commits, but their files, dependencies and terminals stay separate.
TL;DR
One worktree per agent: same repository, separate folder, separate branch.
git worktree add ../myapp-auth -b agent/auth
cd ../myapp-auth && claudeSteps
- 1
Create one worktree per task
Each command creates a new folder with a new branch based on your current HEAD.
bashgit worktree add ../myapp-auth -b agent/auth git worktree add ../myapp-billing -b agent/billing - 2
Start an agent in each folder
Open one terminal per worktree. Each agent only sees its own files.
bashcd ../myapp-auth && claude # second terminal cd ../myapp-billing && codex - 3
Install dependencies per worktree
Untracked files like node_modules and .env are not shared.
bashcp .env ../myapp-auth/.env cd ../myapp-auth && npm install - 4
List what is running
Check every linked folder, its current commit and checked-out branch before starting or removing a session.
bashgit worktree list - 5
Review, merge, remove
Push the branch and open a PR, or merge locally. Then delete the folder.
bashgit worktree remove ../myapp-auth git branch -d agent/auth
Gotchas
- A branch can be checked out in only one worktree at a time.
- Never delete a worktree folder with rm -rf alone; run git worktree prune afterwards.
- Ports collide: give each worktree's dev server its own port.
- Worktrees share the same .git objects, so commits in one are visible in all.
Cheat sheet
| git worktree add <path> -b <branch> | New folder + new branch |
| git worktree add <path> <branch> | Check out an existing branch |
| git worktree list | Show all worktrees |
| git worktree remove <path> | Delete a clean worktree |
| git worktree prune | Clean up stale entries |
Related
Sources
Reviewing agent output? CodeCrab reviews pull requests locally with your own AI tools.
