Topic 1 of 5 · 4 min read
Branch and worktree layouts for parallel agents
Goal: Set up a branch and worktree layout that lets three agents and one person change the same repository without overwriting each other's work.
Give every agent task its own branch and its own worktree, so no two writers ever touch the same files on disk. A branch separates history. A worktree separates the folder. You need both.
Check yourself · 1 of 3
Why does a second agent need its own worktree and not just its own branch?
Why it matters
An agent such as Claude Code, Codex or Cursor edits files in place. If two agents run in one folder, one agent's half-finished edit shows up in the other's git status, and a test run by one fails because of the other. Branches alone do not fix this, because a folder holds one checked-out branch at a time. Switching branches under a running agent rewrites the files it is reading.
How to do it
- Name branches by owner and task. For example
agent/claude/fix-login-redirectoragent/codex/add-csv-export. A reader should see who is working on what, and which branches are safe to delete. - Branch from a fresh main. Run
git fetch originand branch fromorigin/main, not from whatever is checked out. - Add a worktree per task. One command creates both the folder and the branch.
- Keep branches short-lived. One task, one branch, merged within hours or a day. Long branches drift from main and make conflicts bigger.
- Clean up. Remove the worktree when the branch is merged.
git fetch origin
git worktree add -b agent/claude/fix-login ../repo-fix-login origin/main
git worktree add -b agent/codex/csv-export ../repo-csv origin/main
git worktree list
# when merged:
git worktree remove ../repo-fix-login
git branch -d agent/claude/fix-login
All worktrees share one .git object store, so this is cheap: no second clone, no second download. Git refuses to check out the same branch in two worktrees, which is exactly the guard you want.
Before and after
Before: three agents and one person all work in ~/repo on main. Agent two runs the formatter over the whole tree while agent one is mid-edit. The person commits with git add -A and sweeps in both agents' files.
After: four folders, four branches. The formatter run only touches one folder. Each result arrives as a branch you can review and merge on its own.
What goes wrong
- Shared dependencies. Each worktree needs its own install of
node_modulesor a virtualenv, and two dev servers cannot both listen on port 3000. Give each task its own port. - Forgotten worktrees. Dozens of stale folders and branches pile up. List them weekly and prune what is merged.
- Branching from a stale base. An agent that branched last week builds on code that has since changed. Fetch first.
- Untracked files. Git does not copy
.envor other ignored files into a new worktree. Copy what the task needs, and never commit it.
Examples
Three agents and one person
Alice wants a login fix, a CSV export and a docs update done today, and she is also editing the config herself.
git fetch origin
git worktree add -b agent/claude/login-fix ../app-login origin/main
git worktree add -b agent/codex/csv-export ../app-csv origin/main
git worktree add -b agent/cursor/docs ../app-docs origin/main
The main folder stays hers. Each agent is started in its own folder. Three branches come back for review.
Git blocks the double checkout
$ git worktree add ../app-two agent/claude/login-fix
fatal: 'agent/claude/login-fix' is already checked out at '/work/app-login'
This is the safety net working. If a second agent needs the same code, give it a new branch from the first, not the same branch.
Key points
- A branch separates history; a worktree separates the folder on disk, and parallel agents need both.
- Name branches by owner and task so anyone can see who is working on what.
- Always fetch and branch from origin/main, not from whatever happens to be checked out.
- All worktrees share one object store, so extra worktrees are cheap, but each needs its own dependencies and port.
- Remove the worktree and delete the branch as soon as the work is merged.
Common mistakes
- Starting two agents in the same folder and hoping they stay out of each other's files.
- Creating a branch but not a worktree, then switching branches while an agent is running.
- Branching from a stale local main instead of a freshly fetched origin/main.
- Leaving merged worktrees and branches around until nobody knows which are live.
Check yourself · 2 of 3
Which command starts a new branch and a separate folder for one task, based on the latest remote main?
Check yourself · 3 of 3
An agent's new worktree fails its tests with a missing-configuration error, though main works. What is the most likely cause?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The free reference book for branching, rebasing, reset and recovery, written by Git's maintainers' community.
- git-worktree documentation (opens in a new tab)DocsFree
How to give each task its own checkout of one repository, and how to list, lock and remove them.
- git-bisect documentation (opens in a new tab)DocsFree
Exact syntax for bisect start, good, bad, run and reset, with exit-code rules for scripts.