Sub topic 10
Avoiding file conflicts with branches and git worktrees
Parallel subagents share one repository. If two of them edit the same checkout at once, one can overwrite the other's changes, or a test run can see half-finished edits from a different task. This chapter shows how to give each subagent its own branch and directory with git worktree, and where that isolation stops.
You should already be comfortable with Git branches, commits, and merges, and with running shell commands. You should also know that an orchestrator starts subagents with a brief and collects their results. The previous chapter covered when to run subagents in parallel. This one covers how to make parallel runs safe.
The problem: one working directory, many writers
A Git working directory holds one checked-out state: one branch and one set of files on disk. Suppose an orchestrator starts two subagents in the same directory.
- Subagent A edits
README.mdandsrc/cli.py. - Subagent B edits
tests/test_cli.pyand runspytest.
Even with disjoint files, B's tests import src/cli.py while A is still rewriting it. B may report failures that come from A's unfinished work. If both touch the same file, the last write wins and the other change is lost without any warning.
Giving each subagent its own branch is not enough. A branch is a pointer to a commit, and switching branches rewrites the files in the one working directory. Two agents cannot be on two branches in one directory at the same time.
What a git worktree is
A worktree is an additional working directory attached to the same repository. Each worktree has its own checked-out branch, its own files on disk, and its own index (the staging area). All worktrees share one object database, which holds the commits and file contents.
This gives you the properties you want for parallel agents:
- Each subagent edits files in a separate directory, so writes cannot collide.
- Each subagent commits to its own branch, so history stays separate.
- Commits made in one worktree are immediately visible to the others as branches. You do not need to push or fetch.
- Creating a worktree is cheaper than cloning, because history is not copied.
Creating and removing worktrees
The orchestrator, or a script it runs, creates one worktree per parallel task before starting the subagents.
# Run from the main checkout, with main (or your base branch) checked out
git worktree add ../wt-docs -b agent/docs
git worktree add ../wt-tests -b agent/tests
# See what exists
git worktree list
# ...each subagent works inside its own directory...
# After reviewing and merging the branches, clean up
git worktree remove ../wt-docs
git worktree remove ../wt-testsgit worktree add <path> -b <branch> creates the branch from the current HEAD and checks it out in the new directory. To start from a specific base, add it as the last argument, for example git worktree add ../wt-docs -b agent/docs main. Starting every task from the same known commit makes the later merges easier to reason about.
The subagent's runtime must use the worktree path as its working directory. Pass the path in the brief's scope, and also set it as the process working directory if your runtime allows it. A brief that only says "edit src/cli.py" is ambiguous when three copies of that file exist on disk.
Rules Git enforces
- One branch per worktree. Git refuses to check out a branch that is already checked out in another worktree. This protects you from two agents committing to the same branch.
- Branch name collisions. If a branch named
agentexists, Git cannot createagent/docs, because branch names map to paths in.git/refs. Pick a prefix that is not itself a branch name, and keep it consistent across runs. - Dirty worktrees resist removal.
git worktree removefails if the worktree has uncommitted or untracked changes. Use--forceonly after you have checked that nothing there is worth keeping. - Stale entries. If a worktree directory is deleted by hand, Git still lists it. Run
git worktree pruneto remove the stale record.
Collecting the work
Each subagent should commit its changes on its own branch before it reports back. A summary that says "done" with uncommitted files in the worktree is easy to lose and hard to review. Put the instruction in the brief:
Commit your changes on the current branch with a message that describes
the change. Do not switch branches, merge, or push.The orchestrator, or you, then reviews and merges one branch at a time:
git switch main
git diff main...agent/docs # review what the branch changed
git merge --no-ff agent/docs
# run the project's checks on the merged result
git diff main...agent/tests
git merge --no-ff agent/tests
# run the checks again
git branch -d agent/docs agent/testsThe three-dot form main...agent/docs shows changes on the branch since it diverged from main, which is what you want to review. Merging with --no-ff keeps a merge commit, so each subagent's work stays visible as a unit in history and is easy to revert.
Run the checks after each merge, not only at the end. If the tests fail after the second merge, you know the interaction between the two branches is the cause.
What worktrees do not isolate
Worktrees separate tracked files. They do not separate everything a task touches.
| Shared resource | Why it still conflicts | Mitigation |
|---|---|---|
| Merge-time edits to the same file | Two branches can change the same lines, and Git reports a conflict when you merge | Decompose so tasks own distinct files, and say so in each brief's scope |
| Untracked and ignored files | A new worktree has no `node_modules`, virtual environment, `.env`, or build output | Run the install step in each worktree, or copy config files in the setup script |
| Network ports | Two dev servers cannot both bind port 8000 | Give each worktree its own port through an environment variable |
| Local databases and caches | Tests in both worktrees may write to the same database or temp path | Use a per-worktree database name or temp directory |
| Lock files and generated files | Both agents may regenerate `package-lock.json` or a schema file | Assign regeneration to one task, or to the orchestrator after merging |
| Git config and hooks | These live in the shared repository | Do not let subagents change them |
The first row matters most. Worktrees move a file conflict from edit time, where one agent silently overwrites another, to merge time, where Git shows it to you. That is an improvement, because the conflict is visible. It is still work for you, and an agent resolving a conflict between two other agents' changes has to guess their intent. The better fix is upstream: decompose the task so that each subagent owns a distinct set of files.
When you can skip worktrees
Isolation has a setup cost, such as dependency installs and extra disk space. You do not need it in these cases:
- Read-only subagents. Research, code search, and review tasks do not write files. They can share the main checkout, and you can enforce that by removing their write tools.
- Sequential execution. If subagents run one after another, each can work in the same directory on the same branch, because only one writes at a time.
- Tasks that write only to a separate output location, such as a report file with a unique name.
When in doubt, choose the isolation. A leftover worktree costs a little disk space and is easy to remove. A silently overwritten change can cost you an hour of debugging.
Cleaning up
Treat worktrees as temporary. After merging, remove each worktree and delete its branch. If you abandon a task, you can discard it cleanly:
git worktree remove --force ../wt-tests
git branch -D agent/tests
git worktree prune-D deletes a branch even if it is unmerged, so use it only when you have decided to throw the work away. Keeping the branch name and the task name aligned, as in agent/tests for the add-tests task, lets you match a branch in Git to an entry in your run log.
Exercise
You start two parallel subagents. One adds tests in tests/test_cli.py and the other refactors src/cli.py. You give each its own worktree. Both pass their own checks, but after you merge both branches, the test suite fails. Name two reasons worktrees did not prevent this, and one change you would make to the process.
Show answer
Worktrees only isolate the files on disk during editing. First, the tests were written against the old src/cli.py in their own worktree, while the other branch changed that code, so the two branches were never tested together. This is a dependency between the tasks, which means they should not have run in parallel. Second, the merge combined two sets of changes that were each valid alone but incompatible together. Changes to shared resources, such as a regenerated lock file, can cause the same kind of failure. The fix is to run the project's checks after each merge, and to run these two tasks in sequence: refactor first, then write tests against the refactored code.