Skip to content
Guides

Merge conflicts between parallel coding agents: how to avoid them

Run coding agents in parallel without conflicts: assign files, share one base commit, merge in order, preview with git merge-tree and test the result.

Prevent most conflicts before the agents start: give each agent its own set of files, start every branch from the same commit, and keep each task small. For what remains, merge one branch at a time, rebase the others onto the result, preview the next merge with git merge-tree, and run your checks on the combined code. Git only compares text, so the last step also catches the conflicts Git cannot see.

Why do parallel agents conflict?

Two agents conflict when they change the same lines, or when one changes something the other depends on. Agents do not know what the others are doing. If two tasks both touch a shared file, both agents will edit it, each correctly for its own task. The fix is at the planning step, not the merge step.

Split the work by file ownership

Before you start, list the folders or files each task may change, and write that list into the agent's prompt. A task that needs a file owned by another task waits, or you merge that other task first. Then check the boundary after the agent finishes:

# Which files did this branch touch that it does not own?
git diff --name-only main...agent/login-copy | grep -v '^apps/web/src/login/'

Any output is a file the agent changed outside its area. Ask why before you merge. Often it is a rename, a formatter run or a fix that belongs in its own task.

Keep scopes small. A small task touches few files, finishes sooner and conflicts less. Two agents on one large refactor will conflict however you divide it. Run that refactor first, alone, and start the others from the result.

Start every branch from the same commit

Create all branches from one commit, and give each its own worktree so the agents do not share a working folder. The worktrees guide covers the setup around it.

# One branch and one folder per agent, all from the same commit
base=$(git rev-parse main)
git worktree add -b agent/invoice-export .worktrees/invoice-export "$base"
git worktree add -b agent/login-copy .worktrees/login-copy "$base"

A shared start means that every conflict is between branches that you can see, and not a mix of branch changes and changes that landed on main in the meantime. If main moves while the agents work, leave them on the old base until they finish. Rebasing a running agent's branch under it is a source of confusing errors.

Which files conflict most, and what to do about each?

Hot spotWhy it conflictsWhat to do
LockfilesEvery dependency change edits the same fileOne task owns dependency changes. On a conflict, take one side, then run the package manager to regenerate the file; do not merge it by hand
Generated filesOutput of a tool, changed by every task that runs the toolRegenerate after the merge instead of resolving the diff
Migrations numbered by handTwo agents pick the next free number, and Git sees two different filesGive each task a number range, or let one task own all migrations
Shared config, route lists, index filesEvery feature adds one line to the same placeOne owner, or add the lines yourself after the merge

The migration case is the worst of these, because Git reports no conflict: the two files have different names. The problem appears when the migrations run.

Preview conflicts before you merge

git merge-tree --write-tree merges two branches in memory. The documentation says it does not make commits and does not read or write the working tree or the index, so it is safe to run while agents are working. The exit status is 0 for a clean merge and 1 when there are conflicts, and a different value means the merge could not run. The mode first appeared in Git 2.38, so check git --version.

# Exit status 0: clean. 1: conflicts. Nothing is written to your files or index.
for b in agent/invoice-export agent/login-copy; do
  echo "== $b"
  git merge-tree --write-tree --name-only main "$b" || true
done

With --name-only it lists only the conflicting file names. That is enough to decide which branch should go first. To check how a third branch merges after the first two, build the in-memory result into a commit and merge against that:

# Merge two branches in memory, then test a third against the result
tree=$(git merge-tree --write-tree main agent/invoice-export) || { echo conflict; exit 1; }
combined=$(git commit-tree "$tree" -p main -p agent/invoice-export -m "preview")
git merge-tree --write-tree --name-only "$combined" agent/login-copy

The preview commit is not on any branch, and nothing here moves a branch.

Merge one branch at a time, in dependency order

Merge first the branch that others build on, or the one with the most shared files. After each merge, rebase the remaining branches onto the new main and run the full check. A conflict then surfaces while only one change is new, which makes it easy to understand.

git switch main
git merge --no-ff agent/invoice-export
pnpm check                        # your project's full check
cd .worktrees/login-copy
git rebase main                   # the next branch now starts from the new main

Before each merge, review the branch on its own. The branch review checklist covers what to read.

Reuse conflict resolutions with rerere

You rebase the same branches several times, and the same conflict can return each time. git rerere records how you resolved a conflict and applies that resolution when the same conflict appears again, including during a rebase. Turn it on once:

git config rerere.enabled true

The documentation notes that rerere leaves the index alone, so you still review the result with git diff and stage it yourself. Do that review. A resolution that was right last time can be wrong in the new context.

Run checks on the combined tree

A branch that passes its tests alone says little about the merge. After the last branch lands, or on a throwaway integration branch before it, run the complete check: build, type check, lint and the full test suite. Do not run the full suite once per branch in parallel on one machine; run it once on the combined result.

Which conflicts can Git not see?

Git merges text. These pass a clean merge and fail later:

  • One branch renames a function, and another adds a call to the old name.
  • One branch changes what an API returns, and another adds code that expects the old shape.
  • Two branches add the same config key, or two migrations with different numbers that change one table.
  • Two branches add a helper with the same purpose in different files.
  • A test that one branch relies on is removed or loosened by another.

The type checker and the test suite on the combined tree catch the first two. A person catches the rest by reading the final diff against main once, as a whole.

The short version

  1. Assign files to tasks before you start, and check the diff against the list.
  2. Start every branch from the same commit, in its own worktree.
  3. Give hot spots one owner: lockfiles, generated files, migrations, shared config.
  4. Preview with git merge-tree --write-tree, then merge one branch at a time and rebase the rest.
  5. Enable rerere, and run the full check on the combined tree.

Tenslo Code gives each agent its own git worktree and puts the review in the app: every change against the base in one diff, with staging, commits and the pull request in the same place.

Sources

  1. git-merge-tree, Git documentation
  2. Git v2.38.0 release notes, Git project
  3. git-rerere, Git documentation

Share

Each agent on its own branch, reviewed in one diff.

Tenslo Code runs Claude Code and Codex side by side, each in its own git worktree, and shows every change against the base in one diff.