The Branch State Your Coding Agent Forgot to Check
Your coding agent does not know which branch it is on. It thinks it does. It saw a git status output twelve turns ago, it has a CLAUDE.md in its context that mentions the branch name the session opened against, and it watched a tool result list five files that were the right files at the time. The agent has been quietly reasoning against that snapshot ever since. Meanwhile, in a second terminal, you ran git checkout main. The agent's diff lands cleanly on the file system because the OS does not care which branch the bytes belong to. The diff is semantically wrong because the agent's mental model of the branch is stale by three hundred commits and the parent it was reasoning against no longer exists in your working tree.
This is branch-state drift, and it is the coding-agent analog of a read-modify-write race in a database. The agent reads the world at turn N, modifies its plan across turns N+1 through N+k, and writes back to disk at turn N+k+1 — and somewhere in that window the world changed underneath it. No exception fires. No tool returns an error. The patch applies. The harm shows up downstream: a PR opened against the wrong base, a hand-written commit that silently reverts an intervening fix, a feature implemented against a schema that was migrated yesterday.
Why The Filesystem Lies To Both Of You
A git working directory is a projection. The bytes on disk at any moment are the union of the last checked-out commit and whatever uncommitted edits exist in the working tree. The agent's tools — Read, Edit, Bash, Grep — operate on those bytes. None of them carry the parent commit in their result. None of them annotate "this file looks like X because HEAD is currently Y." The agent reads a file at turn 4 and stores its contents in context as if they were stable facts. They were facts about a specific commit at a specific second. By turn 17, both the commit and the second are gone.
The same projection lies to humans too, but humans have peripheral signals — the branch in the shell prompt, the IDE's status bar, muscle memory of what they just typed. Agents have none of these unless someone wires them in. The agent's only window onto branch state is whatever was in its last tool result. If that result is twelve turns old and you have run git pull --rebase since, the agent is operating from a fossil.
What makes this worse than the equivalent human mistake is the asymmetry of recovery. A human who realizes mid-edit that they are on the wrong branch usually catches it from a build error, a missing import, or a name that looks subtly wrong. The agent does not catch it because the agent has no prior of what the branch should look like. The agent's model of "what this codebase contains" is what it has read this session, which is exactly the surface that drifted.
The Three Shapes Of Drift
Branch-state drift shows up in at least three structurally different ways, and the fix for one is not the fix for another.
The first is single-session drift: the user switches branches in another terminal while the agent's session continues. The agent's next file read returns content from the new branch, but the agent's earlier context still describes the old branch. The agent reasons across that contradiction and lands on a diff that fits neither.
The second is multi-agent drift on a shared checkout: two agents run against the same working tree, one switches branches as part of its plan, and the other agent's filesystem view changes underneath it. The second agent reads "uncommitted changes" that are actually the first agent's mid-edit state and tries to be helpful by extending them. The merge becomes incoherent because each agent was reasoning against a different parent.
The third is long-running-agent drift against a moving remote: an agent dispatches a multi-hour task — a refactor, a migration, a sweep across hundreds of files — and by the time it finishes, main has moved twelve commits. The agent's diff was correct against the commit it started from. It is no longer correct against the commit it must merge into. A textual merge succeeds. A semantic merge does not. Tests pass on each side and fail in combination.
Each shape has a different root cause. Single-session drift is a missing turn-prelude. Multi-agent drift is a missing isolation primitive. Long-running drift is a missing rebase-and-revalidate step. Conflating them produces fixes that solve one and miss the others.
Worktrees Are A Floor, Not A Ceiling
The dominant fix in 2026 is the same one git itself shipped years ago for human concurrency: dedicated worktrees. Every agent run gets its own physical directory backed by the shared .git object store. Two agents cannot collide on the working tree because they do not share one. Switching branches in worktree A does not perturb worktree B. The disk cost is trivial — only the working files are duplicated; refs, objects, and remote tracking live once.
- https://code.claude.com/docs/en/worktrees
- https://www.mindstudio.ai/blog/git-worktrees-parallel-ai-coding-agents
- https://www.mindstudio.ai/blog/parallel-agentic-development-claude-code-worktrees
- https://blog.appxlab.io/2026/03/31/multi-agent-ai-coding-workflow-git-worktrees/
- https://www.augmentcode.com/guides/git-worktrees-parallel-ai-agent-execution
- https://www.augmentcode.com/guides/how-to-run-a-multi-agent-coding-workspace
- https://www.gptfrontier.com/preventing-database-and-port-collisions-with-concurrent-ai-agents/
- https://towardsdatascience.com/ai-agents-need-their-own-desk-and-git-worktrees-give-it-one/
- https://www.nrmitchi.com/2025/10/using-git-worktrees-for-multi-feature-development-with-ai-agents/
- https://blog.balakumar.dev/2025/09/25/why-git-worktrees-beat-switching-branches-especially-with-ai-cli-agents/
- https://github.com/manaflow-ai/cmux/pull/671
- https://www.builder.io/blog/ai-agent-orchestration
- https://medium.com/@sathishkraju/the-context-window-is-lying-to-you-and-your-harness-is-the-only-thing-that-matters-33beb165140b
- https://mer.vin/2026/05/agent-harness-and-git-the-same-history-primitives-applied-to-context/
- https://dev.to/thegdsks/cursor-3-ships-parallel-ai-agents-here-is-the-multi-agent-workflow-that-actually-works-2bk8
- https://amux.io/guides/agentmaxxing/
- https://marketingagent.blog/2026/03/22/how-to-use-git-with-coding-agents-a-complete-2026-guide/
