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.

  1. The problem
  2. What a push does
  3. Why telling everyone is not enough
  4. The lock
  5. Checks under the lock
  6. What it does not cover
  7. 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.

chats3–4 on one repo push-mainevery chat runs it lock fileshared git dir checks5, in order CAM journalline after the push origin mainbranch by name loser waits
Figure 1. Every push to main goes through push-main, under one lock.

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.

fetch, redo on top of fix typo a1f3c09setup 7be20d4add header 9c4e1aafix typo chat A · pushed ✓ main e05b3f8add footer chat B · redone, pushed ✓ c81d7e2add footer chat B · push refused ✕
Figure 2. Two chats push main, and GitHub refuses the stale push once.
  1. Chat A adds fix typo on top of add header and pushes. It builds on what main points at, so it lands.
  2. Chat B also built add footer on add header. Its commit leaves out fix typo, so GitHub refuses it.
  3. Chat B fetches, redoes add footer on top of fix 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_EXCL 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:

  1. 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.
  2. Scan every outgoing commit for text that must not leave the machine, not just the newest one.
  3. Push the branch by name, never a commit id and never with --force.
  4. Check origin now holds exactly the commit it scanned. If something landed during the push, it says so and names the unscanned range.
  5. 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 push skips 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.
  1. Block a hand-typed git push while other chats are active, instead of asking.
  2. Make the journal line name the chat that pushed.