How do I run coding agents in parallel without them overwriting each other's changes?

Noor and Ivo at an office water cooler both reach for the same paper cup at once, hands meeting on it; Ivo laughs and Noor gives a wry smile.

TL;DR

Running two coding agents at once usually goes wrong in three places: the files they edit, the runtime they share and the merge. A separate Git worktree per agent is the practical default for the first, because each gets its own checkout and branch. Ports, databases, containers and Git's shared branches and config still need separating by you, and some tasks should simply wait their turn. Agree file ownership and merge order before starting, then test the combined result, because two branches that pass alone can fail together.

Three places parallel agents collide

Two agents working in one checkout share one set of files, one Git index and one current branch. If one of them switches branch, stashes or reformats a file while the other is halfway through an edit, the second agent's work changes underneath it. That is the collision the question names, and most tools now handle it. Two more sit behind it.

In January 2026 someone asked on Hacker News how others run parallel agent sessions. Despite Anthropic's worktree documentation and apps built on worktrees, they still preferred separate clones when they needed to be sure the agents would not overlap. One reply said attention capped them at about three sessions, partly spent wondering how the agents would step on each other at merge time.

In May 2026 a feature request on Claude Code's GitHub asked for configurable worktree behaviour for background agents. One commenter added that many backend projects run on Docker, where a worktree per agent is not practical. A third Ask HN thread put the same problem plainly: multiple checkouts are easy, but they still share one local database, one set of services and one set of ports.

So the work has three layers: files and branches, the runtime, and integration.

Give each agent its own checkout and branch

A Git worktree is a second working directory linked to the same repository. It has its own files, index and HEAD, and Git refuses by default to check out a branch that another worktree already has. Each agent in its own worktree on its own branch no longer edits the other's files in passing. A worktree is a separate checkout, not a security boundary: an agent that writes to an absolute path or into the other directory can still reach it.

The main tools create worktrees for you, each with its own defaults (all checked on 1 October 2026):

  • Claude Code. claude --worktree <name> creates .claude/worktrees/<name>/ on a new branch called worktree-<name>. Background sessions move into a worktree before their first edit, and Claude Code blocks edits and commands aimed at the main checkout while a session is isolated (Claude Code worktrees).
  • Codex app. Worktrees live under $CODEX_HOME/worktrees and start in a detached HEAD from the branch you selected. Work moves back to your checkout through its Hand off feature (Codex worktrees).
  • Cursor. Each task gets an isolated checkout, set up by the commands in .cursor/worktrees.json, and /apply-worktree brings the result into your main checkout (Cursor worktrees).

Two things surprise people. Git shares branches, tags and the repository config file across all worktrees, so an agent that deletes a branch or changes a config value does it for every worktree. And a worktree is a fresh checkout: .env, installed dependencies and build output are missing until a setup step adds them, which is what Claude Code's .worktreeinclude and Cursor's setup file are for.

Separate what the worktree leaves shared

Everything outside the checkout is still shared:

Comparison

ResourceShared between worktrees?How to separate it
Files, index, HEADNoNothing more to do
Branches, tags, repository configYesGive each agent its own branch prefix; no config edits, no force pushes to shared branches
.env, dependencies, build outputNot copiedA per-worktree setup script
Docker Compose containers, networks, volumesIf project names match, and always for explicit names and host pathsA distinct COMPOSE_PROJECT_NAME, then check the resolved config
Host portsYes, one machineA port offset in each worktree's .env
Local databaseYes, if both use the same server and database nameA database name or container per worktree
Staging, sandboxes, queues, cachesYesRead-only access, a per-task prefix, or no parallel writes

The Docker row has a trap. Compose uses the project name to keep environments apart, and without other settings that name comes from the directory, so two worktrees with different folder names get separate containers, networks and volumes (Docker Compose project names). A top-level name: in the Compose file overrides the directory, and then every worktree starts the same project unless COMPOSE_PROJECT_NAME overrides it. A volume with an explicit name: is "used as is and is not scoped" by the project (Compose volumes), an external volume is shared by design, a host bind mount points at the same folder, a fixed container_name collides, and published host ports collide because there is only one host. Run docker compose config in each worktree and compare the project name, volume names, mount sources, container names and published ports. In a test of two directories sharing one file, only the project, network and unnamed volume names differed; the named volume, bind mount, container name and port matched.

When separating the runtime costs more than it saves, run the tasks that need it one after another and parallelise only those that do not. Claude Code added worktree.bgIsolation: "none" for repositories where worktrees are impractical; background sessions then edit your working copy directly (agent view), so give them only tasks that touch different files.

Illustration of Noor and Ivo squeezed side by side in one doorway, each carrying a cardboard box, smiling at each other as neither can get through.
Separate checkouts still meet in the shared runtime: ports, databases and containers.

Agree who owns what before the agents start

Most overwrites that worktrees cannot prevent are planned in, by two tasks that need the same file. Before starting either agent:

  1. List the files each task expects to change. Ask each agent for its plan before it edits, with the files named, and compare the two lists. Any overlap means split the tasks differently or run them in sequence.
  2. Name the hotspots. Lockfiles, database migrations, route and dependency registration, generated clients, test snapshots, translation files and changelogs are touched by almost everything. Give each one a single owner for this round; the other task notes the change it needs instead of making it.
  3. Land shared contracts first. If both tasks depend on a new interface or schema, merge that as its own small change and start both worktrees from it. Claude Code branches worktrees from the default branch unless worktree.baseRef is set to head; Codex starts from the branch you pick.
  4. Fix the merge order and who rebases. The foundational or smaller change merges first. The second rebases onto the updated main branch, regenerates anything ordered or generated (migration numbers, lockfiles), and reruns its checks.
  5. Write the boundary into each prompt. Which files the agent may change, which it must not, and that it stops and asks rather than editing a file owned by the other task. A boundary hard to draw is a warning sign; the article on when parallel coding agents create more work than they save explains why.
Illustration of Noor and Ivo at neighbouring library study carrels; Ivo turns to hold up his handwritten list with one line highlighted, while Noor points her pen at the highlighted line on her own list.
Compare the two file lists before the agents start; the overlap is where the trouble will be.

Test the combination, not just the two branches

Two branches can merge without a conflict and still break each other. As an illustration: task A renames a configuration key and updates every reader it can find, while task B, written against the old code, adds a new reader that uses the old key. Git sees edits in different files, both suites passed in their own worktrees, and the merged application fails on start. Only a check of the combined code catches that:

  • Preview the merge without touching a checkout. git merge-tree --write-tree task-a task-b performs the merge in Git's object store, writes nothing to any working tree or index, and reports conflicts (git merge-tree).
  • Run the full checks on an integration branch holding both changes, in a fresh worktree with its own runtime: the whole test suite, migrations applied to an empty database, and the application started and used along the paths both tasks changed.
  • Let the host test merges in order. A GitHub merge queue groups each pull request with the latest base branch and the pull requests ahead of it, and merges only when the required checks pass on that group; CI workflows must trigger on the merge_group event. It needs an organization-owned public repository, or GitHub Enterprise Cloud for private ones (GitHub merge queue). Elsewhere, requiring branches to be up to date before merging gives a similar check, with the author doing the rebasing.

When the two tasks turn out to depend on each other, a stacked series may suit them better than two parallel branches; the article on turning a huge AI-generated change into pull requests people can review explains how to order dependent pieces.

Paqad does not schedule parallel agents or create worktrees; that isolation stays with Git, your tools and your environment. Within one change, its graduated and full lanes on Claude Code and Codex CLI hand each stage to a fresh worker one at a time, so its own stage workers do not edit the same checkout at once. Since 1.91.1, two sessions in the same project keep separate "framework loaded" markers, and the end-of-turn check examines only the change owned by the session that made it, so work in one window does not block a question in another. Paqad's session continuity page explains what happens when two sessions work in the same project.

Frequently Asked Questions

Start with two agents on tasks you already know are independent, each in its own worktree and branch, before trying more. The limit usually turns out to be your own review and integration time rather than the agents, so more sessions only help while you can still read and merge what they produce.

Besides the files each plan names, look for what the tools write on their own. Run each worktree's setup and build once, then check git status: formatters, code generators and package managers often rewrite shared files that neither plan mentioned.

A worktree is enough when a task's checks need no running service: unit tests with in-memory or mocked dependencies, documentation, refactors inside one module. As soon as a check needs a port, a database, a container or an external account, that resource needs its own copy or a queue.

Write it where the agent will read it: in the task prompt or the plan you approve, not only in your head or a chat with a colleague. A short line per task, such as "owns the billing module and its migration; must not edit the shared router", is enough to make a violation visible in review.

Run both tasks' new tests together on the merged code, because each was written without seeing the other. If either task changed the schema, migrate a copy of the current database as well as an empty one, since an upgrade path can fail where a fresh install passes.