Sub topic 1
Why Parallel Agents Collide in One Checkout
Prerequisites
This chapter assumes you can do the following:
- Use everyday Git: commit, branch, merge, and read a diff.
- Use a shell, including starting a background process with
&. - Run a command-line coding agent on a single task.
You do not need to know git worktree yet. The next chapters introduce it. This chapter explains the problem it solves.
The setup: several agents, one directory
A coding agent is a program that reads your files, asks a language model what to change, edits files, and runs commands such as tests. One agent working on one task is easy to reason about. The appeal of running several at once is obvious: while one agent adds authentication, another can write billing code and a third can improve search. You review the results as they finish.
The simplest way to try this is to open three terminals in the same repository and start an agent in each. That works for a few minutes and then breaks in ways that are hard to diagnose. The cause is that all three agents share one working tree. A working tree is the set of files on disk that you see and edit. A checkout is the combination of a working tree, the index, and a HEAD. Every agent that starts in the same directory reads and writes the same files and the same Git state.
Four ways agents collide
The collisions fall into four groups. Each one comes from something the agents share.
1. Overwritten files
Two agents may edit the same file. Agent A reads src/app.py, plans a change, and writes the file. Agent B read the same file earlier and now writes its own version. B's write replaces A's edit, because most agents write whole files or apply edits against the content they saw earlier.
Neither agent reports an error. A's work simply disappears from the file. Often the loss is only noticed later, when a test fails or a feature is missing.
Even when the agents edit different files, the edits can be inconsistent with each other. Agent A renames a function in utils.py. Agent B, working from its earlier view of the code, keeps calling the old name in a new file. Each edit looks correct in isolation, but the combined result does not compile.
2. A shared `HEAD` and one branch
A checkout has exactly one HEAD and one current branch. Suppose agent A runs git switch -c feature/auth to start its work. Agent B is in the same directory, so its next git commit lands on feature/auth too. Now the history of the auth branch contains billing changes.
If agent B instead runs git switch main or git checkout some-other-branch, the files on disk change underneath agent A. A's next read returns a different codebase from the one it was working on. A may then "fix" the files that suddenly look wrong, which makes the damage worse.
3. A shared index
The index (also called the staging area) is the list of changes prepared for the next commit. There is one index per working tree. When agent A runs git add -A, it stages everything in the directory, including agent B's half-finished work. When A then commits, the commit contains B's partial edits.
The result is a commit with a message about authentication that also contains an unfinished billing change. You can untangle this, but it requires reading the diff carefully, and it undermines the reason for using parallel agents: saving your time.
Git also protects its own metadata with lock files. If two agents run Git commands at the same moment, one may fail with an error like this:
fatal: Unable to create '/path/to/repo/.git/index.lock': File exists.This failure is the mild case. It is at least visible. The agent may react by deleting the lock file and retrying, which can corrupt a legitimate operation in progress.
4. Tests and builds that see other agents' edits
Agents verify their work by running tests, linters, and builds. In a shared directory, those commands run against everything on disk, including other agents' unfinished edits. Agent A's test run fails because agent B is in the middle of a refactor. A then tries to "fix" a failure that A did not cause, and may modify code that belongs to B's task.
Shared generated output causes a related problem. Build directories, caches, and test databases in the working directory can be overwritten or removed while another agent is using them.
| Shared resource | What goes wrong | How you notice |
|---|---|---|
| Working-tree files | One agent overwrites another's edit | Missing changes, no error |
| `HEAD` and current branch | Commits land on the wrong branch, or files change on a branch switch | Mixed history, files that change unexpectedly |
| Index | One agent commits another's staged changes | Commits with unrelated content |
| Build and test output | Tests fail because of someone else's edits | Failures that do not match the agent's own changes |
A short demonstration
You can see the first problem without any agent at all. This script simulates two "agents" that each read a file, make a change, and write it back after a delay. It uses only standard Git and shell commands.
#!/usr/bin/env bash
set -euo pipefail
dir=$(mktemp -d)
cd "$dir"
git init -q -b main
git config user.email "[email protected]"
git config user.name "Demo"
printf 'line 1\nline 2\nline 3\n' > notes.txt
git add notes.txt
git commit -q -m "initial"
agent() {
local name=$1 delay=$2
local content
content=$(cat notes.txt) # the agent reads the file
sleep "$delay" # the model "thinks"
printf '%s\n%s edited this\n' "$content" "$name" > notes.txt
}
agent A 1 &
agent B 2 &
wait
echo "--- final notes.txt ---"
cat notes.txtBoth agents read the same starting content. Agent A writes after one second. Agent B writes after two seconds, using the content it read at the start, so its write replaces A's. The final file contains only B's line:
--- final notes.txt ---
line 1
line 2
line 3
B edited thisA's edit is gone, and no tool reported a problem. Real agents are more careful than this script, because many re-read a file before editing it. A re-read shortens the window for this race, but it does not close it. The window also grows with long model response times.
Why discipline and prompts do not fix this
A common first response is to tell each agent to stay in its own files. That helps, but it has limits.
- Instructions are not enforcement. An agent can still edit a file outside its area when it decides the task requires it, for example to update an import.
- Instructions cannot cover shared Git state. Two agents that both obey "only edit files in
src/auth/" still share oneHEAD, one index, and one set of test results. - Taking turns defeats the purpose. If agents lock the directory and wait for each other, you have serial execution with extra steps.
The right fix is structural: give each agent its own working tree, index, and HEAD, so there is nothing to collide on. Git has a built-in feature for this, and it does not require copying the whole repository.
What a fix has to provide
Before you meet the solution, it helps to list what a good one must do. These requirements shape the rest of the tutorial.
- Separate files. Each agent edits files that no other agent can touch.
- Separate branch and index. Each agent commits to its own branch, with its own staging area.
- Shared history. All agents start from the same commit, and their results can be merged back without exporting or importing patches by hand.
- Low cost. Creating and removing an isolated workspace should take seconds and little disk space, so you can create one per task.
Cloning the repository once per agent satisfies the first three, but it duplicates the object database and makes the clones separate repositories that you must fetch from each other. A Git worktree satisfies all four. It gives each agent a separate directory and branch while sharing a single repository underneath. The next chapter explains how that sharing works.
Not every collision disappears
Separate working trees remove the file, branch, and index collisions. They do not remove every problem. Later chapters cover the remaining ones:
- Two agents can still make changes that conflict when you merge them, because they edited the same lines on different branches.
- Untracked files such as
.env, installed dependencies, and generated output are not copied into a new working tree automatically. - Network ports, databases, and caches outside the repository are still shared by every process on the machine.
- Some Git state, such as hooks and configuration, belongs to the shared repository.
A worktree turns "silent overwrite" into "visible merge conflict", which is much easier to handle. It does not make independent tasks out of dependent ones. Choosing tasks that really are independent remains your responsibility.
Summary
- Agents in one checkout share the working tree,
HEAD, index, and build output. - Collisions produce silent overwrites, commits on the wrong branch, mixed commits, and misleading test failures.
- Prompts that ask agents to stay in their lane cannot protect shared Git state.
- A good fix gives each agent separate files, a separate branch and index, shared history, and low setup cost.
- Git worktrees meet those requirements. Some shared state still remains, and later chapters address it.
Exercise
Two agents run in the same checkout. Agent A runs git add -A && git commit -m "add login" while agent B is halfway through editing billing code. Name two problems this can cause, and explain why telling both agents to "only edit your own files" does not prevent them.
Show answer
First, the commit "add login" will include B's unfinished billing edits, because git add -A stages every change in the one shared working tree and index. Second, A's tests may fail because of B's half-written code, which can lead A to change code outside its task. The instruction does not help because it controls which files an agent edits, not the shared Git state. Both agents still use one index, one HEAD, and one set of files on disk, so staging, committing, and test runs reach the other agent's work regardless.
Exercise
In the demonstration script, agent A finishes first, yet its edit is missing from the final file. Why? Does a "re-read the file before editing" rule in the agent fully fix this?
Show answer
Agent B read the file before A wrote, then wrote the whole file back from that stale copy after A had finished, so B's write replaced A's change. Re-reading before editing narrows the window in which this can happen, but it does not close it. Another agent can still write between the re-read and the write, and the longer the model takes to respond, the larger that window is. Only separate working trees remove the shared file as a point of collision.