Two agents write to the same file, and one agent's commit picks up the other's changes too
Every agent works in its own git worktree. I open each worktree with the Mac's file clone (APFS) instead of git's copy, so the extra disk is close to zero. An agent's commit in the shared working tree is rejected; I keep committing my own notes there.
beforeone shared tree · every worktree a full copy of the repo
after115 worktrees · extra disk per worktree ≈ 0
open a separate git worktree for each agent, don't commit in the shared treeText for your agent · 36 lines
Situation: several agents work in the same repo folder. When one agent commits, the changes another agent staged go into that commit too, or the agents overwrite each other's files.
Why: a git working folder has one index and one set of files. Every agent in that folder shares both: everything staged with `git add` goes into the next commit, no matter who staged it, and agents editing the same file overwrite each other.
Do:
1. Open a worktree of its own for each agent. `git worktree add` writes every file again: each worktree takes as much disk as a full copy of the repo (+200 MB for a 200 MB repo). On a Mac (APFS), don't copy the files, clone them; extra disk ≈ 0. Run this in the shared folder while it is clean (git status --short prints nothing); if it shows `?? .env`, first run `echo .env >> .git/info/exclude` (no commit needed, it ignores .env in every worktree), or the secret lands in the branch with the first commit:
git worktree add --no-checkout ../<agent-name> -b <agent-name>
git ls-tree --name-only -z HEAD | xargs -0 -I{} cp -c -R ./{} ../<agent-name>/
(cd ../<agent-name> && git read-tree HEAD && git update-index -q --refresh)
If it isn't APFS (Linux, Windows) or the repo has submodules, use plain `git worktree add ../<agent-name> -b <agent-name>`.
The new worktree has none of the top-level files git doesn't track (.env, node_modules): copy .env over and reinstall dependencies there.
2. The agent edits and commits only in its own worktree.
3. Reject the agent's commits in the shared folder with a git config hook (it runs even when core.hooksPath or husky has switched off .git/hooks). The hook only rejects an agent's environment (Claude Code, Codex); you keep committing there from your terminal:
git config hook.shared-gate.event pre-commit
git config hook.shared-gate.command 'i="${GIT_INDEX_FILE:-index}"; case "${i##*/}" in index|index.lock) ;; *) exit 0;; esac; [ -z "${CLAUDECODE:-}${CLAUDE_CODE_SESSION_ID:-}${CODEX_THREAD_ID:-}" ] || [ "$(git rev-parse --path-format=absolute --git-dir)" != "$(git rev-parse --path-format=absolute --git-common-dir)" ] || { echo "no agent commits in the shared folder: commit in your own worktree" >&2; exit 1; }'
The hook is shared by every worktree; its condition singles out the shared folder (only there is git-dir = git-common-dir). `git commit --no-verify` skips the hook; a commit given a path (`git commit -- <path>`) also gets through it and picks up what someone else staged on that path: tell your agent to use neither in the shared folder.
I measured this hook form on git 2.54 (git --version). Older git doesn't know config hooks, and these two lines do nothing without any error: commits in the shared folder go through. If your git is older, update it first (brew install git), then run this step; the git hook run line under Verify shows that the hook really runs.
4. Land work on the main branch one at a time. If two landings run at once, one fails with "index.lock: File exists", or the other agent's file can stay staged in the shared folder's index (it stayed in 33 of 40 tries on my machine, 0 of 50 on another); so serialize landing with a lock (the function's definition and its call run in the same command; agent shells don't carry functions from one command to the next):
land() { L="$(git rev-parse --path-format=absolute --git-common-dir)/land.lock"; got=; for n in $(seq 300); do mkdir "$L" 2>/dev/null && { got=1; break; }; sleep 0.2; done; [ -n "$got" ] || { echo "landing lock busy for 60 s: $L (delete it if no landing is running)"; return 1; }; git merge --ff-only "$1"; rc=$?; rmdir "$L"; return $rc; }
git -C ../<agent-name> rebase <main-branch>
(cd <shared-folder> && land <agent-name>)
Only this landing runs in the shared folder. If it says "Not possible to fast-forward", another landing got in first: go back to the rebase step; don't use the `merge --no-ff` git suggests.
5. When the work is done:
git worktree remove ../<agent-name>
git branch -d <agent-name>
If remove says "modified or untracked files": delete the file if it is junk; if it is work, commit in that worktree first, land it with step 4, then remove it. Don't use --force or branch -D: work that hasn't landed on the main branch is lost. (The .env you copied is deleted too; the original .env stays in the shared folder.)
Verify:
- in the new worktree, after copying .env, git status --short → empty
- git worktree list → number of running agents + 1 lines (the first line is the shared folder); a finished worktree must not stay in the list
- git -C <shared-folder> status --short → empty
- the agent runs git hook run pre-commit in the shared folder → "no agent commits in the shared folder" and exit 1 (runs the hook without committing); run by you in a terminal, the same command exits 0
- in an agent's worktree git hook run pre-commit → exit 0
- git hook list --show-scope pre-commit → "local shared-gate" (shows up even when core.hooksPath or husky has switched off .git/hooks)
Don't: give the same worktree to two agents; git doesn't stop this, you do.Also in: If you work with many agents