How to Run Several AI Coding Sessions in One Git Repo Without Overwriting Each Other

When several AI sessions work in one repo at the same time, two things go wrong most often: the git staging area they all share, and the working copies each session creates on its own. Writing "be careful" in a rules file does nothing. The rules have to be check scripts that stop the command before it runs.

A dozen or so of my apps, several websites and some internal tools live in a single repo. The upside is that manuals, scripts and rules are written once and every new product reuses them. Each of the three rules below was added after something broke.

Commit only the files you changed

When two sessions share one staging area, whoever commits second takes along whatever the first one staged. This happened to me once. One session was committing a version bump for an app. Another was renaming every product directory and had just staged more than two thousand renames. The first session's commit swept up all of those half-finished changes.

The rule is that every commit lists the files you changed, one by one, at the end of the command, with paths written from the repo root, for example 10-apps/FocusFlow/STATUS.md. Staging first and then committing with no paths is not allowed. To enforce this I added a pre-commit check script. If there are no file paths, or only a bare file name that does not start from the repo root, it stops the commit and prints the correct form.

The check script belongs in the global config. The first time I put it only in the config at the repo root, so a session launched from a subdirectory never loaded it, and another commit got swept up.

Create worktrees in one fixed place and delete them when done

When an AI needs to work in isolation, it creates a copy of the repo, edits there, and merges back to main. Without limits, those copies end up scattered, and nobody cleans them up after the merge. At one point I had more than ten full-repo copies piled up over a few days, each two or three GB.

The rule is that a copy may only be created in a fixed folder under the matching product directory, with only the needed parts checked out, and the session that created it deletes it after merging back to main. A check script blocks any copy in the wrong location, too.

Start testing at the cheapest layer

After an AI fixes a bug, it is tempting to go straight to a real device and then ask me to verify. One feature once took six fix tickets in two days, and twice I had to pick up my phone to confirm something I could have learned by reading the code.

Now the order goes from cheapest to most expensive: read the code, unit tests, simulator, a development phone, and only then a person. A question that a lower layer can answer does not get sent to a higher one. When a batch of tests passes, stop, and if you suspect a flaky result, rerun at most once. Problems found are sorted into just two kinds, "blocks release" and "note it for later", and the second kind does not get its own fix ticket. Before a release I run one more automated audit. What it checks is in Chapter 2, Step 9 (in Chinese).

What to do

  1. When several sessions share a repo, ban commits without paths. Enforce it with a pre-commit hook script and put it in the global config.
  2. Write paths from the repo root, never bare file names.
  3. Create working copies in one fixed place, delete them right after merging, and check regularly for strays.
  4. Use English for directory names and for paths inside scripts, and do not hard-code Chinese directory names in scripts.
  5. Layer your testing by cost and put the human at the last layer.
  6. Every time something breaks, ask whether a script could have stopped it. If so, do not leave it only in the rules file.

Sources

  • Shared staging area incident notes, July and August 2026
  • Standard skeleton and testing strategy for new projects

Related

One email on Sundays with what I wrote that week: how I build and ship apps with AI, what it costs, and what went wrong. Unsubscribe any time.

RSS

I'm Jiang. I build apps with AI after my day job and write down how it goes. Four of the apps are free and open source on GitHub.