---
title: "How to stop Claude Code agents from overwriting each other's changes · Cork"
description: "Why parallel Claude Code agents overwrite each other's edits, and how to stop it with worktrees, file ownership, deny rules, PreToolUse hooks and a write gate."
canonical: "https://corkcli.com/blog/stop-claude-code-agents-overwriting-changes"
last_updated: "2026-11-14"
published: "2026-10-03"
author: "Kevin Vera"
---

# How to stop Claude Code agents from overwriting each other's changes

Why parallel Claude Code agents overwrite each other's edits, and how to stop it with worktrees, file ownership, deny rules, PreToolUse hooks and a write gate.

By Kevin Vera · October 3, 2026 · [Guides](https://corkcli.com/blog/c/guides) · 6 min read

Claude Code agents overwrite each other when two of them write to the same files in the same checkout, or when one runs a git command that throws away another's uncommitted work. To stop it, give each agent its own git worktree, assign each agent the files it owns, deny destructive git commands, and enforce ownership with a `PreToolUse` hook so an out-of-bounds edit is blocked rather than discouraged. If agents must share a checkout, you need something that holds an edit while another agent is changing the same code.

## How agents overwrite each other

It helps to know the three ways work gets lost, because each needs a different fix.

1. **Same file, same checkout.** Agent A reads `api.ts`, agent B edits it, agent A writes its version based on what it read. B's change is gone, and neither agent knows.
2. **Destructive git commands.** An agent tidying up runs `git checkout -- .`, `git stash` or `git reset --hard` in a checkout that also holds another agent's uncommitted edits.
3. **Merges that silently drop intent.** Each agent's branch is fine, but when they merge, one side's change undoes or breaks the other's. Git only flags this when the same lines changed.

Claude Code's own docs acknowledge the first case for agent teams, which share one checkout: "Two teammates editing the same file leads to overwrites. Break the work so each teammate owns a different set of files."

## Fix 1: one worktree per agent

The strongest protection against the first two cases is physical separation. Start each session in its own git worktree:

```bash
claude --worktree api-refactor
claude --worktree ui-polish
```

Each gets its own directory under `.claude/worktrees/` and its own branch. As of October 2026, while a session is isolated this way Claude Code blocks `Edit` and `Write` calls that target the main checkout, and blocks git commands redirected into it with `git -C`, `--git-dir` or a `cd`. For subagents, add `isolation: worktree` to the subagent's frontmatter so each one gets a temporary worktree.

Worktrees have a cost: each is a fresh checkout, so you reinstall dependencies and copy gitignored files (Claude Code's `.worktreeinclude` file handles the copying). They also do nothing about case 3, which is covered below.

## Fix 2: assign file ownership up front

Whether or not you use worktrees, decide before starting which agent owns which paths, and put it in the prompt: "You own `src/billing/**` and its tests. Do not edit anything else; if you need a change elsewhere, tell me." Ownership by module is the single biggest reducer of conflicts, because it removes the overlap rather than managing it.

## Fix 3: deny destructive git commands

Add deny rules to `.claude/settings.json` so no agent can run the commands that discard other people's work:

```json
{
  "permissions": {
    "deny": [
      "Bash(git reset *)",
      "Bash(git stash *)",
      "Bash(git clean *)",
      "Bash(git push --force *)"
    ]
  }
}
```

Claude Code applies deny rules even when the command is nested inside a subshell or a `for` loop. Treat these as guardrails, not guarantees: the permissions docs note that a rule like `Bash(git push *)` does not match a spelling such as `git -C . push origin main`, so pair them with worktrees.

## Fix 4: enforce ownership with a PreToolUse hook

A prompt asks; a hook enforces. A `PreToolUse` hook runs before each tool call, and exiting with code 2 blocks the call and sends your stderr message back to Claude so it can adjust. This script, saved as `.claude/hooks/own-billing.sh`, blocks edits outside `src/billing/`:

```bash
#!/bin/bash
file=$(jq -r '.tool_input.file_path // empty')
case "$file" in
  */src/billing/*) exit 0 ;;
  *) echo "This session owns src/billing/ only. Ask the user before editing $file." >&2
     exit 2 ;;
esac
```

Wire it up for file-editing tools:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/own-billing.sh" }
        ]
      }
    ]
  }
}
```

Because each agent owns different paths, put each agent's hook in a settings file you pass to that session with `--settings`, rather than in the shared project settings. Path-based permission rules such as `"deny": ["Edit(/src/ui/**)"]` are a lighter alternative when the boundary is simple.

## Fix 5: rebase and test before every push

Case 3 is the one isolation cannot fix. When an agent finishes, rebase onto the latest main and run the tests before pushing:

```bash
git fetch origin && git rebase origin/main
# run the test suite
git push
```

The rebase shows textual conflicts one branch at a time. The test run catches the quieter kind, where agent A renamed a function and agent B's new code still calls the old name. Land branches one by one so each later agent rebases onto what already landed.

## How Cork helps

Everything above either separates agents or relies on rules you wrote in advance. None of it tells an agent, while it is working, that the function it is about to edit is being changed by someone else right now. Cork is the [coordination layer](https://corkcli.com/blog/coordination-layer-ai-coding-agents) for multiplayer coding agents: it keeps Claude Code, Codex and Cursor agents, on one machine or across a team's machines, from overwriting each other on one codebase, and hands each agent what the others already learned.

For overwrites specifically:

- **[Write gate](https://corkcli.com/blog/write-gate):** an edit against code another agent is changing right now is held until your agent has their version. That closes case 1 even in a single shared checkout, without a worktree per agent.
- **Live awareness and [semantic lint](https://corkcli.com/blog/semantic-lint):** when another agent renames a function or changes a signature yours depends on, your agent hears it on its next step, and the change becomes a lint rule your agents are checked against. That targets case 3 before the merge, not after.
- **Landing:** `git push` becomes one step that rebases onto teammates' commits, reruns the tests and pushes.
- **Agent messaging:** any agent can message the others, one agent, or whoever is working on a given function.

Hooks in Claude Code, Codex and Cursor run this with no extra model calls. Cork Free covers one machine with no account; the paid plan adds teammates' machines. Install:

```bash
curl -fsSL https://corkcli.com/install.sh | sh
```

## Frequently asked questions

### Why did Claude Code undo my other agent's changes?

Most often the agent wrote a file based on a version it read before the other agent's edit, or ran a git command such as `git checkout -- .` that discarded uncommitted work. Separate worktrees prevent both.

### Does Claude Code lock files while an agent edits them?

Not across separate sessions. Agent teams use file locking for claiming tasks, but the docs still advise that teammates own different files to avoid overwrites.

### Are worktrees enough to stop overwrites?

They stop overwrites while agents work. They do not stop one branch from breaking another at merge time, so you still need a rebase plus tests, or live notice of changes.

### Can a hook block Claude from editing a file?

Yes. A `PreToolUse` hook with an `Edit|Write` matcher that exits with code 2 blocks the edit and tells Claude why.

## Sources

- <https://code.claude.com/docs/en/worktrees>
- <https://code.claude.com/docs/en/agent-teams>
- <https://code.claude.com/docs/en/permissions>
- <https://code.claude.com/docs/en/hooks-guide>
- <https://code.claude.com/docs/en/sub-agents>

## Related reading

- [Cork Free: set up multiplayer coding agents on one machine in one command](https://corkcli.com/blog/cork-free-setup-one-machine): Set up Cork Free in one command so Claude Code, Codex and Cursor agents on one machine stop overwriting each other. No account; data stays on the machine.
- [Cork vs Xirp: run many agents, or keep many agents from colliding](https://corkcli.com/blog/cork-vs-xirp): Cork vs Xirp: Spotify's Xirp runs parallel coding agent sessions in worktrees on macOS. Cork coordinates those agents so they stop overwriting each other.
- [How to stop an agent editing a file someone else is changing](https://corkcli.com/blog/stop-agent-editing-file-someone-else-is-changing): Block an AI coding agent from editing a file another session is changing: a tested Claude Code PreToolUse lock hook, Cursor and Codex equivalents, and limits.
- [Cork vs Superconductor: a hosted agent workspace, or coordination for the agents you run](https://corkcli.com/blog/cork-vs-superconductor): Cork vs Superconductor: Superconductor runs coding agents in cloud sandboxes for teams. Cork stops parallel agents from overwriting each other's code.

[All articles](https://corkcli.com/blog.md) · [Cork in Markdown](https://corkcli.com/index.md) · [llms.txt](https://corkcli.com/llms.txt)

This page in HTML: https://corkcli.com/blog/stop-claude-code-agents-overwriting-changes
