# Why parallel AI agents overwrite each other's files, and how to stop it

> Parallel AI coding agents overwrite each other's files because they share one working tree: one folder, one git index and one checked-out branch, with nothing stopping two of them from changing the same file. The fix is to give each agent its own copy of the project, usually a git worktree, and to merge their work back one at a time.

Source: https://gettenfold.app/guides/ai-agents-overwriting-files · By Romain Delage, maker of Tenfold · Published 2026-11-20 · Updated 2026-11-20

Running several coding agents at once is one of the best ways to get more done with them. It's also the fastest way to lose work, if they all run in the same folder. This guide explains what actually goes wrong, then the fixes, from plain git to tools that do it for you.

## What goes wrong in a shared folder

When you start two agents in one checkout of a repository, they share three things: the **working tree** (the files on disk), the **index** (what's staged for the next commit) and **HEAD** (the branch that's checked out). Nothing in git or in most agents stops two of them from using those at the same time.

That leads to a handful of failures, roughly in order of how often people hit them.

### Edits based on an old copy

An agent reads a file, decides on a change, then writes it. If another agent changed the file in between, the second change is based on an old copy. Depending on the agent and the edit, it either fails and has to re-read the file, or it quietly undoes the other agent's work. The second case is the one that hurts, because nothing tells you it happened.

### Formatters and code generators

Many projects run a formatter, a linter with auto-fix or a code generator over the whole tree. When one agent runs it, it rewrites files another agent is editing. The other agent then sees changes it didn't make, and often "fixes" them back.

### Shared files every task touches

Some files change in almost every task:

- **lockfiles**, like `package-lock.json` or `Cargo.lock`, rewritten whenever a dependency changes;
- **database migrations**, often numbered in sequence, so two agents both create "migration 42";
- **registries**: route tables, dependency injection setup, barrel files that re-export modules;
- **changelogs and config files** at the root of the project.

Two agents touching these at once is a conflict even when their real work is in different places.

### Git commands that affect everyone

This is the one that surprises people most. Agents run git commands, and in a shared folder those commands affect every agent:

- `git add -A` or `git commit -a` stages and commits **another agent's half-finished changes**;
- `git checkout` or `git switch` changes the branch, and every file with it, **under the other agent's feet**;
- `git stash`, `git reset --hard` or `git checkout -- .` can throw away another agent's uncommitted work entirely.

### Edits that clash in meaning

Even when two agents never touch the same file, their work can clash. One renames a function, and the other adds three new calls to the old name. Each change is fine on its own. Together, the build breaks.

## How to stop it

### 1. Give each agent its own git worktree

A git worktree is a second (or third, or tenth) folder for the same repository, checked out on its own branch. Each worktree has its own working tree, its own index and its own HEAD, while sharing the repository's history. It's built into git, and it's the standard fix.

```sh
# From your main checkout: one folder and one branch per agent
git worktree add ../myapp-search -b agent/search
git worktree add ../myapp-billing -b agent/billing

# Start an agent in each folder
cd ../myapp-search && claude
```

A few things to know:

- **Each worktree needs its own setup.** Untracked files like `.env` aren't copied, and dependency folders like `node_modules` aren't shared. Install and copy what the project needs to run.
- **Dev servers need different ports** if several agents run the app at once.
- **A branch can only be checked out in one worktree at a time.** Give each agent a new branch.
- **Clean up when you're done** with `git worktree remove <path>`, and list them with `git worktree list`. Git won't remove a worktree with uncommitted changes, so you can't lose work that way.

The [guide to git worktrees for Claude Code](https://gettenfold.app/guides/claude-code-worktrees) goes through this step by step.

### 2. Split the work by area

Worktrees stop agents from overwriting each other while they work. They don't stop conflicts at merge time. The more two agents' changes overlap, the bigger the merge.

So give each agent a clear area: the search feature, the billing page, the API client. Say it in the prompt, including what not to touch. Tasks that cut across the whole codebase, like a rename or a dependency upgrade, are best done by one agent while the others wait, or done first.

### 3. Give shared files one owner

Decide which agent owns each shared file. For lockfiles, it's often simplest to let agents change the dependency manifest, then regenerate the lockfile once after merging. For migrations, let one agent create them, or renumber them at merge time.

### 4. Keep git commands out of shared folders

If agents do share a folder, tell them not to run commands that affect the whole tree: no `git add -A`, no branch switching, no stash or reset. With Claude Code, the place for that is the project's `CLAUDE.md`. It's a safety net, not a guarantee, which is why worktrees come first.

### 5. Merge one at a time

When agents finish, merge their branches into main one by one. After each merge, bring the remaining branches up to date with the new main (`git rebase main` in each worktree, or merge main into them) and run the tests there. Conflicts show up one branch at a time, while they're small, instead of all at once at the end.

### 6. Review as they go

The earlier you see what an agent is changing, the earlier you see it heading for another agent's area. Watching the diff as it's written is much cheaper than untangling two finished branches. More on that in [reviewing AI-written code as it's written](https://gettenfold.app/review-ai-code).

## How Tenfold handles it

Tenfold is a Mac app for running coding agents side by side, starting with Claude Code, and it builds these fixes in.

**It notices overlaps as they happen.** When two agents edit the same file in the same checkout, both get flagged: the Board tile says who else is editing the file, the Focus inspector shows an "Overlapping edits" banner naming it, and the Code layout counts and lists the overlaps. The banner has a **Give one its own copy** link that starts the second agent in a copy of its own.

**It gives each agent its own copy in a few keys.** In the New agent sheet (⌘N), choose **Its own copy** under **Where it works**, name the branch and press ⌘↩. Each copy is a git worktree, kept under `~/.tenfold/worktrees/` so it stays out of your project folder. When another agent already works in that repository, Tenfold marks its own copy as recommended.

**It compares agents in separate copies too.** When two agents touch the same files or lines on their branches, the git panel says so before either lands, with a line like "Pogo and Sprig are editing the same lines" and a "Will clash with Pogo" chip on the agent's card.

**It lands work carefully.** When an agent is done, bring its work into main from its Board tile or the git panel. Tenfold checks for clashes with main first. If there are any, you settle them side by side in the agent's own copy, and main isn't touched. It can test the result on a fresh copy of main before it lands, then removes the agent's copy and keeps a backup of the branch for 30 days. If main is protected on GitHub, it opens a pull request instead.

It also recognizes Claude Code sessions in worktrees you made yourself with `git worktree add`, so you can mix both ways of working. For the full picture, see [running multiple Claude Code agents in parallel](https://gettenfold.app/run-multiple-claude-code-agents).

## Questions

### Can two Claude Code agents work in the same repository at the same time?

Yes, but not safely in the same folder. Two agents in one folder share the working tree, the git index and the checked-out branch, so they can overwrite each other's edits, commit each other's half-finished work, or switch the branch out from under each other. Give each agent its own git worktree, a separate folder of the same repository on its own branch, and they can work in parallel without colliding.

### How do I stop AI agents from editing the same file?

Run each agent in its own git worktree, so their edits land in separate folders and on separate branches. Then split the work so each agent owns a different area of the code, give shared files like lockfiles and migrations to one agent, and merge the branches back one at a time, rebasing the others onto main after each merge.

### Do git worktrees prevent merge conflicts?

No. Worktrees stop agents from overwriting each other while they work, but if two agents change the same lines on different branches, you still get a conflict when you merge the second one. Splitting the work by area and merging early keeps those conflicts small.

### Why did my agent commit another agent's changes?

Because they share one git index. In a single folder, a command like `git add -A` stages every change in the working tree, including the files another agent is in the middle of editing. Separate worktrees each have their own index, so a commit in one never picks up work from another.

## Read next

- https://gettenfold.app/guides/claude-code-worktrees
- https://gettenfold.app/run-multiple-claude-code-agents
- https://gettenfold.app/review-ai-code
- https://gettenfold.app/guides/agentic-development-environment

---

Tenfold is a native Mac app for Claude Code, with live code review, git and the notch built in. https://gettenfold.app/
