Skip to content

Catch merge conflicts between parallel agents early.

Agents in separate worktrees never collide while they work. They collide at merge time, when two branches changed the same lines. You can find that out before either one is merged, while the order is still your choice.

Applies to Claude Code, Codex, OpenCode, Cursor Agent, Gemini CLI and any CLI agent6 min read

When conflicts happen

Each worktree is a branch from main. Two branches conflict when both change the same lines, or one deletes a file the other edits. Git only tells you during the merge, and by then the first branch is already on main and the second agent has moved on to other work.

Check two branches by hand

Since git 2.38, git merge-tree --write-tree runs a merge in memory without touching any worktree. It exits with status 1 when the merge would conflict, and --name-only lists the conflicted files.

Terminal
git merge-tree --write-tree --name-only --no-messages refund-status-badge table-contrast
echo $?

It prints the tree the merge would make, then each file the merge would stop on, such as src/components/StatusBadge.tsx. An exit status of 1 means a conflict, 0 a clean merge, and any other status that git could not run the check, for example because a branch name was mistyped.

The same check against main tells you whether a branch still merges cleanly now that main has moved:

Terminal
git fetch origin
git merge-tree --write-tree --name-only --no-messages origin/main refund-status-badge

Check every pair

With several agents, check every pair of branches. Run it after each agent commits, or from a git hook.

check-conflicts.sh
#!/bin/bash
# Your agents' branches.
branches=(refund-retries ledger-export webhook-signatures)
for a in "${branches[@]}"; do
  for b in "${branches[@]}"; do
    [[ "$a" < "$b" ]] || continue
    out=$(git merge-tree --write-tree --name-only --no-messages "$a" "$b")
    case $? in
      0) ;;
      1) echo "$a and $b conflict in:"; tail -n +2 <<< "$out" ;;
      *) echo "git merge-tree failed for $a and $b" >&2; exit 2 ;;
    esac
  done
done
Only what is committed

merge-tree compares commits. Changes an agent has not committed yet are invisible to it, so run it again after each commit.

Choosing the order

When two worktrees conflict, merge either one first. The other then merges main into its branch and resolves the conflict there, where its agent knows why it made its change. Asking the agent that wrote the code to resolve it is usually faster than resolving it yourself.

By hand, in the worktree that lands second: commit its edits first, merge main, fix and stage each file git status lists as conflicted, finish the merge and run the tests. If the first branch landed only on your local main, merge main instead of origin/main.

Terminal
cd ~/code/payments-api-ledger-export
git status --short
git fetch origin
git merge origin/main
git status
git add src/components/StatusBadge.tsx
git merge --continue
pnpm test

With Dockazor

Dockazor watches for this across every worktree of a project, and against main. When two worktrees change the same lines, the sidebar marks both with ⚠ before either is merged, uncommitted and new files included, and its tooltip names the file: “Merge either worktree first; the other can then resolve against it.”

  • Changes, against main, shows the blocks a merge with main would stop on, as of now: this worktree’s lines and main’s, one above the other.
  • Resolve Conflict… hands a conflict with main to the worktree’s agent, in its chat.
  • Merge into main… lands the work once the conflict is gone.