Contents
Stop AI agents overwriting each other's changes
Each agent gets its own worktree, so no agent writes over another agent's files. gitta then watches the worktrees together and tells an agent when another agent changed code its work relies on, so it adapts instead of undoing the other change.
Why agents overwrite each other
Two things cause it. When several agents share one folder, each one edits the files the others just changed. An agent that reloads a file and finds unexpected code often "fixes" it back. People who run agents in parallel describe the same thing, with agents fixing one bug in opposite directions (DEV, running Claude Code agents in parallel).
The second cause is later. Each agent works from the commit it started on. When one agent's work is merged and another agent's work is merged after it, the second can bring back the old code of a function, or call it in its old form.
1. Give each agent its own worktree
A git worktree is a second working folder of the same repository, with its own branch. Agents in separate worktrees cannot write over each other's files.
$ git worktree add -b ci-level ../ci-level
$ git worktree add -b ci99 ../ci99Claude Code, Cursor and Codex can make the worktrees for you. See Git worktrees with Claude Code, Cursor and Codex.
2. Let gitta watch all the worktrees
$ gitta init --gate "python3 -m unittest discover -s bench/aidev-replay -q"From now on, gitta compares what each agent changed with what the other agents' new code uses.
| What happens | What the agent hears |
|---|---|
| Another agent changed a function, type or constant that this agent's new code uses | A warning at its next step with the other agent's new definition |
| Both agents changed the same function or the same lines | An also editing note. gitta merges both changes by structure. |
| Work reached main that this agent's code relies on | Nothing while it works. gitta checks the change at merge time. If the tests fail, the merge output shows the new definition on main. |
3. Merge with git
Merge each agent's work with git merge, as always. gitta runs your tests on the combined code during the merge. When they fail, the merge stops before main moves, so a late agent's work meets the tests together with what an earlier agent changed.
$ git merge --no-edit ci99
Auto-merging bench/aidev-replay/analyze.py
gitta: tests fail on main with ci99 (test_ci99.T.test_wider); main unchanged, merge cancelled (ignore git's note). Fix it and merge again.
TypeError: wilson() takes 2 positional arguments but 3 were given
Now on main: bench/aidev-replay/analyze.py:23: def wilson(k, n, *, z=1.96):
Not committing merge; use 'git commit' to complete the merge.This example comes from the quick start.
Agents that open pull requests get the same comparison from the pull request check.