Sub topic 1
Why parallel agents collide in one checkout
Prerequisites
This chapter assumes you can:
- Commit, branch, merge and diff in Git.
- Use a shell, including running a command in the background with
&. - Run a command-line coding agent on a single task.
You don't need to know anything about git worktree yet. This chapter explains the problem that worktrees solve.
One checkout, one set of files
A Git checkout has three parts that matter here:
- The working directory holds the files you edit.
- The index holds the changes staged for the next commit.
HEADpoints to the branch or commit that the working directory reflects.
Each checkout has exactly one of each. When you work alone, this is fine, because you do one thing at a time. A coding agent is a process that edits files, runs tests and sometimes runs Git commands. If you start two agents in the same directory, both act on the same working directory, the same index and the same HEAD.
Three ways agents collide
Overwritten files. Two agents that touch the same file write to the same bytes on disk. The last write wins, and the earlier agent's work disappears without any Git warning, because nothing was committed.
Mixed changes. Agent A edits auth.py while agent B edits search.py. Neither overwrites the other, but git status and git diff now show both sets of changes together. When either agent runs git add -A and commits, it commits the other agent's half-finished work too.
Shared branch state. If one agent runs git switch -c agent/auth, the files on disk change for every agent in that directory. Another agent may be editing a file at that moment and now sees different content than it expects. Git also guards the index with a lock file, so two simultaneous Git commands can fail with Unable to create '.git/index.lock': File exists.
A small demonstration
This script simulates two agents rewriting one file at the same time.
mkdir collide && cd collide
git init -q -b main
echo "base" > notes.txt
git add notes.txt
git -c user.name=demo -c [email protected] commit -qm "base"
(echo "auth change" > notes.txt) &
(echo "search change" > notes.txt) &
wait
cat notes.txt
git status --shortnotes.txt ends up with only one of the two lines, and the order depends on timing. Git reports a single modified file and has no record that two different agents tried to change it.
What the collision looks like
Why the usual fixes fall short
- Taking turns. Running agents one after another is safe, but it removes the speed benefit of parallel work.
- Telling agents to stay out of each other's files. Instructions don't control which files a process writes to, and the shared index and
HEADremain shared anyway. - Cloning the repository several times. This works. It copies the whole history for each agent, and each clone has its own remotes and settings to keep in sync.
What you want is a separate working directory, index and HEAD for each agent, while keeping one shared history. Git provides this with linked worktrees, which the next chapter explains.
Exercise
Agent A edits auth.py and agent B edits search.py in the same checkout. Neither agent touches the other's file. Name two ways they can still interfere with each other.
Show answer
Any two of these. Agent A's git add -A and commit will include agent B's unfinished edits. Either agent switching branches changes the files on disk for both. Simultaneous Git commands can fail on the index lock file. Both agents also see each other's changes in git status and git diff, and a test run by one agent may fail because of the other agent's half-written code.