After a batch of parallel agents, your commits land on the wrong branch, and your branches were cut from a base that no longer exists
The shared checkout is to blame, not git worktree. The two drift mechanisms that follow a batch of agents, and the commands that expose them.
In short
- Symptom
- Git shows no error. Yet the current branch is not the one you thought, some commits come from another agent, and the merge bases are older than the current
main. - Real cause
- Two separate mechanisms: a shared
HEADwhen agents create their branch inside the main checkout, and base drift when each worktree freezes a different base whilemainmoves on. - Fix
- Require an isolated workspace in every agent's prompt, cut from a named base, on a unique branch name. Then run the end-of-batch audit before you merge anything.
- Prevention
- An audit script run after every batch, a
pre-commithook in the main checkout, and a base that does not move while the batch runs.
- Updated
- 2026-10-14
- Read time
- 9 min
- Access
- open
The answer, in three lines
The culprit is the shared checkout: the main directory of the repository, when every agent
works in it at the same time. git worktree has nothing to do with it. An agent that runs
git checkout -b in that directory moves HEAD for everyone. And a worktree created while the
base was moving commits on top of a stale state.
The two mechanisms are not fixed the same way. You fix the first one by naming the workspace in
the agent's prompt. The second one only shows up in an audit, with git merge-base and the list of
files that several branches changed. Run that audit before you merge.
The symptom
Git shows no error message. Nothing warns you before integration, and that delay is what makes this failure expensive.
What you see instead, in no particular order:
$ git branch --show-current
fix/agent-3-pagination # I thought I was on main
$ git worktree list
/home/dev/mon-app 3a4b5c6 [fix/agent-3-pagination]
# one checkout. For six agents.
$ git log --oneline main..fix/agent-1-exports
a1b2c3d fix: pagination des exports
e4f5g6h feat: écran de rattrapage # this commit is not agent 1's
$ git merge-base fix/agent-4-i18n main
1a2b3c4 # main has been at 7e8f9a0 since this morning
The most expensive symptom does not show at all: two branches edited the same file, git merges them without a conflict, and the merged file is wrong.
The wrong diagnosis
The second wrong diagnosis is harder to spot: "the agents picked the wrong branch". Every agent did exactly what it was told. The defect is in the instruction, not in how the agent carried it out.
The third one burns the most time later on: "merge conflicts are normal, we're running things in parallel". Independent tasks should produce no conflicts at all. A conflict is a signal, and treating it as a routine cost is the mistake.
What is actually happening
Two separate mechanisms go through the shared checkout, and each one has its own fix.
Mechanism 1: the shared HEAD
If you tell an agent "create a branch fix/x" without requiring a worktree, it runs
git checkout -b fix/x in the main checkout. That moves HEAD for every other agent, and for you.
Between one agent's checkout -b and its commit, another agent has time to move HEAD again.
The first agent then commits onto the second one's branch. Each agent's uncommitted files end up in
the others' working tree, and a careless git add picks them up.
I saw this on one project: four agents writing code were in fact sharing a single directory, and
git worktree list never showed more than one checkout. Commits had to be made hunk by hunk, one
block of changed lines at a time, to avoid carrying a neighbour's work. Two test failures came from
no agent at all: the test runner was reading files that other agents were rewriting while it ran.
Mechanism 2: base drift
This one hits even when every agent has its own worktree. Worktrees share the same .git, but
each one has its own base, frozen when it is created. A worktree created while main was
moving therefore commits on top of a stale base.
On one batch, meaning a group of agents started at the same time, I found eight agent branches
created from eight different bases (1a2b3c4d, 5e6f7a8b, 9c0d1e2f, 3a4b5c6d, …). All of those
bases were older than the current main, and all of them were already merged into it. The eight
branches were redoing a rename that was already in place, in an incomplete version: their base did
not yet contain a spec file, so their patch-id differed from that of the correct version. They
added nothing, and they would have introduced a regression had they been merged.
This is the more dangerous variant. The branch is green and its diff reads well, but it takes the code back to where it was three hours ago.
What makes both worse
Two situations come up every time. If the requested branch name is already taken, the agent quietly
falls back to some technical branch, and you only find out at integration time. And your own
uncommitted changes on main during the batch are the root cause of both mechanisms: main moves
after the agents have already left.
The fix
Step 1: Require the worktree and the base in every agent's prompt
Work in an ISOLATED git worktree, base = clean `main`.
git worktree add ../wt-<unique-name> -b <unique-branch> main
Never `git checkout -b` in the main checkout.
Never switch branches in the main checkout.
Never commit a file you did not modify yourself.
Step 2: Before starting the batch, one command on your side
git status --porcelain # must be empty. Otherwise commit or stash FIRST.
Step 3: The post-batch audit, pass or fail
This audit is often skipped. It is also the step that prevents the most damage.
# 1. the main checkout has not moved
git branch --show-current # must print main
# 2. every agent did get its own worktree
git worktree list # one line per agent, not one line total
# 3. no branch was cut from a stale base
for b in $(git branch --list 'fix/*' 'feat/*' --format='%(refname:short)'); do
printf '%-40s base=%s main=%s\n' "$b" \
"$(git rev-parse --short "$(git merge-base "$b" main)")" \
"$(git rev-parse --short main)"
done
# any line where base != main is drift: rebase before integrating.
# 4. THE check that avoids the worst — file overlap between branches
comm -12 <(git diff --name-only main..fix/agent-1 | sort) \
<(git diff --name-only main..fix/agent-2 | sort)
On a batch of five agents, that last command found two collisions, on files changed by two different agents. Git would have merged them automatically, without a conflict, and wrongly. No test catches this kind of breakage: each branch is green on its own, and nobody tests the result of the merge.
When two branches look like they do the same thing, compare their patch identities rather than their diffs:
git log --format=%H main..fix/agent-1 | xargs -n1 git show | git patch-id --stable
Two identical patch-id values mean a harmless duplicate. Two different ones for the "same" change
mean two versions of the same work, one of them stale.
Prevention
- An audit script, run by hand after every batch: the four commands above, with a non-zero exit if any of them fails. It fits in twenty lines.
- In the repository instruction file, the worktree instruction spelled out in full. The branch name is imposed by whoever starts the agent, and the agent does not choose it.
- A
pre-commithook in the main checkout that refuses any commit whengit rev-parse --abbrev-ref HEADis notmainwhile agent worktrees exist. In practice, this is what catches mechanism 1. - A written integration procedure: the merge order decided before merging, a review of the merges git made automatically, and a worktree set aside for the integration itself.
Why this matters with agents
This failure does not exist with a single agent. It is the only one in the notebook with that property, which is why it sits at the centre of the product.
It has something the others do not: its cost arrives later, and it grows in the meantime. Nothing breaks during the batch. The agents finish, report green tests and hand in credible summaries. The bill arrives at integration time, when you have to decide, branch by branch, what is new work and what brings back an old state. Across eight agent branches, none of them added anything, and two would have introduced a regression.
What this entry does not cover
Shared state outside git (a cache, a volume, an install-state file written by two processes) produces similar symptoms, for a different reason.
The git index lock, held by two agents at once, needs its own entry. It is not published yet.
Where this entry comes from
It comes from two shared-HEAD incidents and one batch of eight drifted branches, on personal
repositories, between February and August 2026. The commit ids quoted here are fictitious: they
only illustrate the eight distinct bases that were observed.
This entry is part of a notebook of 27 production failures hit between February and September 2026. Some are public and free: see the notebook.
The others are published as they are written, along with the modules and their exercises, as part of the paid product. In that product, the modules are in English; the exercises and the other entries are in French. Nothing on this page is a teaser for it.