Sub topic 10
Avoiding file conflicts with branches and git worktrees
This chapter assumes you know basic Git (branches, commits, merges) and the command line. It also assumes you have read the earlier chapters on the orchestrator-worker pattern and on running subagents in parallel.
The problem: one directory, many writers
Parallel subagents that share one checkout edit the same files on disk. If two of them touch src/cli.py, the last write wins. A test run by one subagent can also fail because another subagent is halfway through an edit.
A branch alone does not fix this. A normal repository has one working directory with one branch checked out. If one subagent runs git switch, the files change under every other subagent.
Git worktrees
A worktree is an extra working directory linked to the same repository. Each worktree has its own checked-out branch, its own index, and its own files. All worktrees share one object database, so creating one is fast and uses little disk space.
Setting it up
The orchestrator creates one worktree and one branch per subagent, then points each subagent at its own directory.
# Give each parallel subagent its own checkout and branch
git worktree add ../wt-docs -b agent/docs
git worktree add ../wt-tests -b agent/tests
# ...each subagent works inside its own directory...
# Review and merge each branch from the main checkout
git merge agent/docs
git merge agent/tests
# Clean up
git worktree remove ../wt-docs
git worktree remove ../wt-tests
git branch -d agent/docs agent/testsUse git worktree list to see every linked worktree and its branch.
What isolation does and does not cover
| Concern | Isolated by a worktree? |
|---|---|
| Tracked files being overwritten | Yes |
| Uncommitted edits seen by other agents | Yes |
| Merge conflicts when two branches change the same lines | No |
| Untracked or ignored files such as `.venv` or `node_modules` | No, each worktree starts without them |
| Ports, databases, and other shared resources | No |
Common errors
- Branch already checked out. Git refuses to check out the same branch in two worktrees. Give each subagent its own branch name.
- Missing dependencies. A new worktree has no virtual environment or installed packages. Run your setup step in each worktree before the subagent starts, or its tests will fail for reasons unrelated to its task.
- Removal fails.
git worktree removestops if the worktree has uncommitted changes. Commit or discard them first. Do not delete the directory by hand, because Git will keep stale metadata until you rungit worktree prune. - Conflicts at merge time. Isolation delays collisions to the merge step. Reduce them by giving each subagent a scope of different files, as described in the brief chapter.
Merge one branch at a time and run the tests after each merge. If a merge conflicts, the orchestrator can resolve it or send the conflicting task back to a single subagent to redo after the first branch lands.
Exercise
Two subagents each run in their own worktree on branches agent/a and agent/b. Both edited the same function in src/cli.py. Which problem is gone, and which one remains?
Show answer
Overwriting each other's files on disk is gone, because each worktree has its own copy. The merge conflict remains, because Git still has to combine both changes to the same lines when the branches are merged. Resolve it at merge time, or run the two tasks in sequence.