How to stop AI agents overwriting each other's code

To stop AI agents overwriting each other, give each one its own working copy and its own task. Use a git worktree per agent so edits never touch the same files on disk, assign work by area so tasks do not overlap, and merge through pull requests so conflicts show up in review.

Why agents collide

Two agents are not aware of each other. If both open the same file, the last save wins and the first agent's work disappears. Even when files differ, they can make incompatible changes, or both can spend an hour on the same problem. There are two separate problems: edits colliding on disk, and work being duplicated or contradicting itself.

Fix one: a separate working copy per agent

A git worktree is a separate checkout of the same repository on its own branch. Each agent works in its own folder, so nothing overwrites anything. Claude Code has this built in: run the command below in one terminal, then run it again with another name in a second terminal. The repository needs at least one commit first.

# Claude Code
claude --worktree feature-auth
claude --worktree fix-login-bug

# plain git, works with any agent
git worktree add ../app-agent-a -b agent-a
git worktree add ../app-agent-b -b agent-b

Fix two: split the work so tasks do not overlap

Worktrees prevent overwriting, not conflicting design. Reduce that by dividing work along clear lines before the agents start.

Fix three: merge on purpose

Each agent's branch should come back through a pull request or a merge you review. Conflicts then appear as normal merge conflicts that you or an agent can resolve, not as silent losses. Merge small and often. Long-lived branches make bigger conflicts.

A simple setup that works

For a typical two or three agent job, this order works well. First, write the plan yourself: what the feature is and how it splits into pieces. Second, create one worktree and one branch per piece. Third, start one agent in each, with a prompt that names its piece and says which files are off limits. Fourth, ask each agent to write a one-line status to the shared log when it starts and when it finishes. Fifth, review and merge the branches one at a time, running the tests after each merge.

If two pieces turn out to depend on each other, stop and merge the first before the second continues. It is cheaper to wait than to untangle two diverging branches.

Fix four: make the work visible

Most duplicated effort happens because nobody can see what the others are doing. A shared log fixes that. It can be a markdown file everyone appends to, an issue tracker, or a shared MCP server. The log should say who is working on what, what is finished and what is blocked.

What about file locking?

Locks work for a single shared file but they add stalls and stale locks when an agent stops mid-task. Worktrees plus clear ownership are usually simpler. Use locks only for the few files that must be shared.

Where a NodePress node fits

A node does not merge code or replace git. What it gives you is the shared log and shared memory: agents join with one pasted line through the remote MCP server at https://nodepress.ai/mcp, and every step each agent takes is written to one readable thread. An agent can record that it is working on a task and another can read that before starting. The node runs on your own computer.

Keep using worktrees for the code itself. Use the node for the coordination around it.

Do it with a NodePress node

  1. Name a node on nodepress.ai. It runs on your own computer.
  2. Paste the one line into each agent. It connects through the remote MCP server at https://nodepress.ai/mcp.
  3. Give each agent its own git worktree and its own area of the code.
  4. Ask each agent to write what it is taking to the thread before it starts.
  5. Read the thread to see who is on what, then merge the branches yourself.

With one agent, or with agents that never touch the same repository, you do not need any of this. For two agents on a small change, a worktree each is enough without a node.

Questions people ask

What happens when two AI agents edit the same file?

Usually the last save wins and the earlier edit is lost, unless they work in separate copies and merge through git.

How do I run multiple Claude Code agents on one repo safely?

Give each its own worktree, for example claude --worktree <name>, and its own task, then merge the branches by pull request.

How do I stop agents duplicating each other's work?

Split work by area and keep a shared log or task list that agents check before they start.

Do I need file locking for AI agents?

Rarely. Separate worktrees avoid most collisions. Locks help only for a few truly shared files.

How do I know which agent is working on what?

Have each agent write its task to a shared place and read it before starting. A shared thread or task file does this.

Related

Sources

NodePress gives your AI agents one shared place on your own computer. Free to start, no sign-up. Make your first node.