One worktree per agent
Several AI agents in one checkout are several developers sharing one keyboard. How git worktree, two checks before apply and an early push protect you from someone else's branch, stale plans and lost work.
With one agent in a repository, a shared checkout goes unnoticed. With two or three — in neighbouring terminals, in the background, on a schedule — the working copy becomes shared mutable state. And we all know how shared mutable state without synchronisation ends.
An agent doesn't see what its neighbour is doing. It ran git checkout a minute ago and is sure it's on its own branch. It has no reason to re-check before every step — from its point of view, nothing has changed.
What broke
Everything below comes from one week of several agents working in a single infrastructure repository.
- The branch changed underfoot. In one session the shared checkout ended up on someone else's branch twice: another agent switched it for its own task. The first one kept working without noticing.
- A commit landed in someone else's work. Changes were committed to the neighbour's branch and shipped in their merge request.
- Hours spent comparing configurations. While branches kept changing, comparing "before" and "after" was pointless: each time the comparison was against something else.
- The worst one — Terraform. It reads local configuration but keeps shared state. After a branch switch, the plan was built from someone else's configuration against our state — and proposed deleting DNS records and a service access rule. Had it been a background
apply -auto-approve, public access would have disappeared silently.
In none of these cases was the agent's logic wrong. Its premise was: "the working copy is mine."
The rule: one task, one worktree
git worktree gives you a separate working directory with its own branch, sharing the object store. It's cheaper than a clone and more honest than a stash.
git fetch
git worktree add .worktrees/<topic> -b feature/<topic> origin/masterEach agent gets its own directory and works only there. Now it physically cannot switch a neighbour's branch.
One detail: anything not in Git doesn't move to the new worktree. .env, *.tfvars, kubeconfig, .terraform have to be copied by hand — once per worktree.
Isolation doesn't protect against staleness
A worktree protects you from neighbours, not from time. A branch created a week ago can be hundreds of commits behind — and still produce a perfectly valid plan against a configuration that no longer exists.
So before any action that depends on the directory contents — apply, deploy, commit, image build — run two checks:
git branch --show-current
git rev-list --left-right --count origin/master...HEADThe first answers "where am I", the second "how true is that still": it prints two numbers — how many commits the branch is behind origin/master and how many it is ahead. The second matters just as much: an isolated but stale branch is more dangerous than a shared one, because it looks safe.
Check after apply too
Master moves while you apply. In one day, full plans showed something unexpected four times:
- twice — deletion of other people's resources because of stale branches;
- once — applying someone else's work that hadn't been applied yet;
- once — deleting services created five minutes earlier in another commit.
-target would have hidden every one of these. It narrows not only the action but also the visibility: you won't see what else would have changed. A targeted apply is sometimes needed to avoid touching other people's resources — but read the full plan first.
Isolation is not storage
An unpushed branch lives on one disk. Once, six commits from two sessions lived only locally for two days and reached the remote as a single new branch. One disk failure, and two days of work from two sessions would have been gone.
Push as soon as the work is worth keeping, not when it's finished. A wip/<topic> branch on the remote gets in nobody's way.
What a worktree doesn't solve
A worktree gives isolation, not coordination. Someone else's unmerged branch can still block your task: during one audit, a needed runner fix was waiting on another agent's branch. Working around it locally means two diverging worlds. Talk to the branch author — even if the author is an agent too.
Checklist for AGENTS.md
Copy this into your project's AGENTS.md or CLAUDE.md — the agent will read it before starting work:
## Working copy
1. One task — one `git worktree` from a fresh `origin/master`:
`git fetch && git worktree add .worktrees/<topic> -b feature/<topic> origin/master`.
Copy secrets and local state (`.env`, `*.tfvars`, kubeconfig, `.terraform`) by hand.
2. Before apply, deploy, commit or image build, check where you are and how fresh it is:
`git branch --show-current` and `git rev-list --left-right --count origin/master...HEAD`.
3. Read the full plan; use `-target` only after that, and deliberately.
4. Check the state after apply too: master may have moved on.
5. Push the branch as soon as the work is worth keeping, not when it's finished.Agents don't have to remember they're not alone. It's enough that the environment doesn't let them forget.