Git Worktrees for Parallel AI Agents: Two Writers on One Index Swapped Files
By Vladimir Mikhalev Docker Captain · CNCF Ambassador · IBM Champion Blueprint 15 The landing order: parallel agents count once their work is in main, shaped and in a chosen order
Two writers committing to one working tree put the other writer’s file into 333 of the 333 commits they made. The only difference from a run where 19 commits did was a pause of 200 milliseconds between git add and git commit, and with a git worktree each the same test produced none.
Running several AI coding agents in one checkout is the default most setups start from, because it is the checkout that is already open. A working tree has one index, the staging area every git add writes to and every git commit reads, and two agents in one tree share it the way two people would share one clipboard. I run a race in CI that measures what that costs, with two scripted writers, because a race shows up in thousands of tries, not in a dozen agent sessions.
What two writers on one index look like
The test is public: the workflow, the writer and its counters, and a test that plants each finding before the counters are trusted. Two writers, a and b, start on the same instant in a fresh repository. Each makes 20 commits per repeat, one new file per commit in its own folder, and commits as its own author, so a commit is attributed by who made it rather than by its message. Every writer runs the same cycle:
# scripts/worktree-index-race.py in fleet-ops at 3aac0e4, lines 188 to 196: call("status", "status", "--porcelain") _, kind = call("add", "add", "--", rel) if kind is not None: rec["outcome"] = "add_failed_" + kind log["commits"].append(rec) continue if args.gap_ms: time.sleep(args.gap_ms / 1000.0) p, kind = call("commit", "commit", "-m", subject)A call that fails on the index lock, on a ref lock or on an empty message is retried up to ten times, so the run measures what a careful agent would see, not one that gives up. The worktree mode gives each writer its own tree and its own branch:
# scripts/worktree-index-race.py in fleet-ops at 3aac0e4, lines 286 to 289: for w in WRITERS: wt = os.path.join(root, "wt-" + w) git(repo, "worktree", "add", "-q", "-b", "agent-" + w, wt) dirs[w] = wtThree configurations ran on 5 November 2026, each in both modes, on the versions in the second table below (no pause, 10 repeats, a 200 ms pause, 10 repeats, no pause, 50 repeats; the counts are in the result file):
| One tree / a worktree each | 10 repeats, no pause | 10 repeats, 200 ms pause | 50 repeats, no pause |
|---|---|---|---|
| Commit attempts | 400 / 400 | 400 / 400 | 2000 / 2000 |
| Commits made | 382 / 400 | 333 / 400 | 1876 / 2000 |
| Commits carrying the other writer’s file | 19 / 0 | 333 / 0 | 125 / 0 |
| Repeats with at least one | 8 of 10 / 0 | 10 of 10 / 0 | 46 of 50 / 0 |
| Commits under the other writer’s message | 0 / 0 | 0 / 0 | 7 / 0 |
| Own file already committed by the other | 18 / 0 | 67 / 0 | 120 / 0 |
| Files lost, in no commit at all | 0 / 0 | 0 / 0 | 4 / 0 |
| Index lock failures, retried | 18 / 0 | 17 / 0 | 180 / 0 |
| Ref lock failures, retried | 0 / 0 | 1 / 0 | 24 / 0 |
| Empty-message aborts, retried | 1 / 0 | 0 / 0 | 1 / 0 |
| Merge of the two branches, median | n/a / 22.9 ms | n/a / 40.0 ms | n/a / 33.3 ms |
No writer gave up after ten retries, the merges had no conflicts, and in worktree mode every counter stayed at zero across all 2,800 commits. A re-run on 24 November 2026, on git 2.56.1 (no pause, 10 repeats, a 200 ms pause, 10 repeats, no pause, 50 repeats; the counts are in the result file at that point), gave other counts, because the race depends on timing, and the same picture: commits carrying the other writer’s file in every one-tree configuration, and none with a worktree each.
| What ran | Version |
|---|---|
| git on the CI runners, Ubuntu 24.04.5, 4 CPUs | 2.56.0 |
| Claude Code, headless, model claude-opus-5-5 (local run) | 2.1.283 |
| GitKraken Desktop, its gk CLI 3.1.79 (local run) | 12.6.2 |
| GitLens on VS Code 1.144.0 (local run) | 19.5.0 |
The pause is the uncomfortable column. An agent that stages a file, then thinks about its commit message, leaves its file in the shared index long enough for the other agent’s commit to take it. Every one of the 333 commits made with that pause carried a file its author never staged, and 67 times a writer found its own work already committed under someone else’s name. The history looked healthy. Each commit had a sensible message and a file in it, just not the right file. For a team that reviews or audits by commit author, the record is wrong at the source: the author named on the commit did not write the file inside it.
A shared index does not fail loudly. It attributes work to the wrong author and keeps going.
Four files that git add accepted and no commit kept
In the run with 50 repeats, four files were lost. The writer’s git add returned zero, the file then left the index, and the writer’s own commit said “nothing added to commit but untracked files present”. No commit in the history contains those files. They were still on disk, untracked: lost from the history, not from the machine. In all four cases the other writer’s status, add and commit cycle spanned the lost add: it started about two milliseconds before it and its commit returned about seven milliseconds after.
That timing is consistent with the order in which git commit handles the index for a plain commit. It reads the index into memory first:
// builtin/commit.c in git at v2.56.0, lines 392 to 393: if (repo_read_index_preload(the_repository, &pathspec, 0) < 0) die(_("index file corrupt"));and only later takes the index lock, refreshes, and writes the copy it holds back:
// builtin/commit.c in git at v2.56.0, lines 483 to 491: repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR); refresh_cache_or_die(refresh_flags); if (the_repository->index->cache_changed || !cache_tree_fully_valid(the_repository->index)) cache_tree_update(the_repository->index, WRITE_TREE_SILENT); if (write_locked_index(the_repository->index, &index_lock, COMMIT_LOCK | SKIP_IF_UNCHANGED)) die(_("unable to write new index file"));An add that lands between the read and the write-back is in the index for a few milliseconds and then is not. An older release from the 2.50 line has the same order. This is an observation from a race the writers were built to provoke, not a bug report, and I have not reported it to the git mailing list. What it shows is the cost of the habit: the index is one file, and two processes that each read it, change it and write it back can each be right and still lose the other’s change.
What real agents did, and why it is not a control
In a local run, two headless Claude Code sessions worked in parallel on a clone of the same repository, each asked to write twelve short notes and commit each one as soon as it was written: git add that one file, then git commit. Eight runs, four in one tree and four with a worktree each, made 192 commits. No log had an index lock or ref lock error, no commit carried the other agent’s file, and no note was lost.
The reason is in how they committed. In all 8 shared-tree sessions the agent named its file on the commit line itself, although the prompt said to add that file first and then commit. With a worktree each, 1 of 8 did. A commit that names its path is built from HEAD plus that path, so the other agent’s staged files cannot get in.
# the form, not a log line: what the prompt asked for, then what the agents rangit add notes/agent-a/note-01.md && git commit -m "agent-a: note 01"git commit -m "agent-a: note 01" -- notes/agent-a/note-01.mdThat is a habit of one model on one day, not a property of the setup. In an earlier run, one agent read the race script’s own header, saw the index lock described there, and wrote itself a retry loop; on a clone without those files, none of the eight did. An agent that happens to commit by path is safe from the other agent’s staged files. An agent that follows the instruction it was given is not.

Two Claude Code sessions started from one worktree card in the Agents view of GitKraken Desktop, in the version listed above; both are waiting on Claude Code’s folder-trust prompt. “Running” is GitKraken’s label.
What the git tools do when the index is locked
Desktop tools now start agent sessions themselves, and they meet the same lock. In my run on GitKraken Desktop, the version in the table above, staging a file while another process held the index lock failed at once with the toast “Staging paths failed” and git’s own text, and it did not retry after the lock was released:

In my run on GitLens, whose changelog for that release says it now waits and retries when another process briefly holds the index lock, the add waited about 750 milliseconds and then ran once. With the lock released 0.4 seconds after the click, it completed. With the lock held for five seconds, it failed with git’s lock error, shown in the GitLens output log and nowhere in the interface, and it did not try again.

GitLens (Git) output log, lock released 0.4 seconds after the click; time of day masked.

GitLens (Git) output log, lock held five seconds, then the same add with no lock; time of day masked, lines between the two parts removed.
Neither tool can make one index safe for two writers. What GitKraken does offer is the fix: worktrees as a first-class view, and an Agents view that starts a session per worktree card.
The tools retry the lock. Only a separate index removes the race.
Framework: one agent, one worktree, one branch
Layer 1: give every agent its own worktree and branch (week 1)
One worktree per agent session, created before the session starts, on a branch named for the agent or the task:
# a sketch, not in the repository: one tree, one index and one branch per agentgit worktree add -b agent-a ../wt-agit worktree add -b agent-b ../wt-b# when both are done, in the main tree:git merge --no-edit agent-a agent-bgit worktree remove ../wt-a && git worktree remove ../wt-bEach worktree has its own index and its own HEAD; they share the object store, so a worktree costs a checkout’s worth of disk, not a clone’s. In GitKraken the same thing is a worktree card with a session started from it.
Owner: the engineer who launches the agents, with the team lead setting it as the default for any run of more than one agent.
Layer 2: integrate on purpose, and let the merge be the review point (week 2)
The integration step the shared tree skipped is now explicit. In the race it took a median of 22.9 to 40.0 milliseconds with no conflicts, because the writers touched separate folders; real agents will conflict, and the merge is where a person sees that two agents changed the same file, instead of finding it later inside a commit with the wrong author. Keep the agents’ branches until the merge is reviewed, and delete the worktrees only after.
Owner: the reviewer who merges the agents’ branches.
Layer 3: measure the habit, not the incident (week 3)
The failure is timing-dependent, so a single clean run proves nothing. Run the race on the git version your runners use, in both modes, and keep the result file. In a fork of the repository, or with the workflow copied into your own:
gh workflow run worktree-index-race.yml \ -f repeats=10 -f commits=20 -f think_ms=50 -f gap_ms=200The counters to watch are the ones a healthy-looking history hides: commits with the other writer’s file, commits under the other writer’s message, and files lost from the index. In a shared tree any of them above zero means the setup is not safe for parallel agents, whatever the agents happened to do on the day.
Owner: the platform engineer who owns the CI runners and the agent tooling.
Tradeoffs
A worktree per agent costs disk for each checkout, a branch to name and clean up, and a merge that a shared tree pretends not to need. Hooks and configuration in the main repository apply to every worktree, which is usually what you want, and a tool that assumes one working directory per repository may need to be pointed at the right one.
Committing by path, the habit the agents showed, is a narrower fix. It keeps the other agent’s staged files out of your commit, but the agents still take turns on one index and one lock file, and it depends on every agent and every person in that tree doing it every time. The worktree takes the decision out of the agent’s hands.
A checkout’s worth of disk per agent, against commits nobody can attribute. Pay for the disk.
The closing argument
One working tree is one index, and one index is a shared clipboard for every process that stages a file. Two writers on it produced commits with the wrong files, commits under the wrong messages and files that git accepted and no commit kept, while every individual git command reported success. Give each agent its own worktree before it starts, and make the merge the place where their work meets.
Sources
- heyvaldemar, fleet-ops worktree race (the writers, the counters and the result file), November 2026
- git, git worktree and the source of git commit at the tag the runs used, accessed November 2026
- GitKraken, Worktrees in GitKraken Desktop and Agents, accessed November 2026
- GitKraken, the GitLens changelog of 27 October 2026, accessed November 2026
Discussion
If you run more than one coding agent against the same repository, or you have watched a commit land with a file its author never staged, drop a comment below. Counterarguments are welcome and the comment thread is where I respond first.