Serializing git pushes across parallel AI agents
in Multi-agent git
When several AI chats can push the same branch, two can push at once and leave origin holding two different copies of the same history.
- The problem
- What a push does
- Why telling everyone is not enough
- The lock
- Checks under the lock
- What it does not cover
- Summary
The problem
On a busy day, several Claude chats work in the same repository. Each one commits its own files to main, and at some point one of them pushes. If two push at the same moment, origin can end up holding two different copies of the same history. A split like that happened to us once, from 27 to 29 September: chats pushed a copy of their commit by its id instead of pushing the branch, and GitHub's history drifted from ours until a merge joined them.
You don't need anything special to follow along. The idea works for any team where several people, or several agents, can push the same branch.
The accent arrow is a second chat finding the lock taken.
What a push does
Each commit points back to the one before it, and main on GitHub is a label on the newest one. A push asks GitHub to move that label to your commit. GitHub agrees only if your commit builds on the one the label is on now, so nothing already there gets dropped.
- Chat A adds
fix typoon top ofadd headerand pushes. It builds on whatmainpoints at, so it lands. - Chat B also built
add footeronadd header. Its commit leaves outfix typo, so GitHub refuses it. - Chat B fetches, redoes
add footeron top offix typo, and pushes again. That one lands.
That refusal is the safety net. The rest of this article is about making sure the second chat never gets as far as trying.
Why telling everyone is not enough
Every chat writes to a shared journal we call CAM, and every chat reads it at the start of a session. A push shows up there as one line:
14:20 session push pushed main → origin — pushed main aa5c3d4..cb186fc (8 commits)
Notice the timing: that line is written after the push has landed. It's a record, not a warning. A chat that started pushing a second earlier would never have seen it. A message can tell others what happened; it can't stop two things happening at once. For that you need something that refuses.
The lock
Nobody pushes main by hand. Every chat runs one script, push-main, and the first thing it does is create a lock file inside the repository's shared git directory:
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
O_ makes the operating system create the file only if it doesn't exist yet, in a single step. Two chats can't both win. The loser stops straight away with "another push-main holds the lock, wait for it". Because the lock sits in the shared git directory, it covers every worktree of the repo, not just one folder. A lock older than ten minutes is treated as left behind by a crashed run and replaced, with a warning.
Checks under the lock
Holding the lock, the script does the rest in order:
- Fetch, then insist on being strictly ahead. If origin has commits the local branch doesn't, it refuses and says to merge first. A chat that queued up behind the first push gets turned away here instead of pushing over it.
- Scan every outgoing commit for text that must not leave the machine, not just the newest one.
- Push the branch by name, never a commit id and never with
--force. - Check origin now holds exactly the commit it scanned. If something landed during the push, it says so and names the unscanned range.
- Only then write the journal line.
Behind all of this, GitHub refuses any push that isn't a fast-forward, so even a push that got past everything else can't overwrite someone's commits.
What it does not cover
- A hand-typed
git pushskips the lock. A guard asks before running one while other chats are active, but it asks rather than blocks, so a yes gets through. - The lock only exists on this machine. A clone elsewhere is protected by GitHub's fast-forward rule alone.
- The journal line didn't name which chat pushed. It fell back to a generic name, so the record is less useful than it should be.
Summary
- A shared journal tells everyone what happened. It can't prevent a race.
- One script owns the push, and an exclusive lock file lets only one copy of it run.
- Under the lock: fetch, require being strictly ahead, scan, push by name, confirm what landed.
- The remaining gap is the hand-typed push, and the next step is to block it instead of asking.
- Block a hand-typed
git pushwhile other chats are active, instead of asking. - Make the journal line name the chat that pushed.