Sub topic 11
Resolving Conflicts Between Agent Branches
This chapter assumes you can resolve an ordinary merge conflict by hand and that you have followed the earlier chapters. You should have one worktree and one branch per agent, such as agent/auth, agent/billing and agent/search, all started from main. You should also have reviewed an agent branch with git diff main...agent/auth.
Where conflicts come from
Worktrees stop agents from overwriting each other's files while they work. They do not stop two branches from changing the same lines. Each agent commits against its own copy of history, and the disagreement shows up when you bring the branches together.
Conflicts fall into two kinds:
- A textual conflict happens when two branches change the same region of a file. Git cannot choose between them, so it stops and writes conflict markers into the file.
- A semantic conflict happens when the merge is clean but the result is wrong. For example, one agent renames
load_usertofetch_user, and another agent adds a new call toload_user. Git sees edits in different places and merges without complaint. The code then fails at runtime.
Textual conflicts are noisy and easy to catch. Semantic conflicts are quiet, so you need tests after every merge to find them.
Preview conflicts before you merge
You do not need to start a merge to find out whether it will conflict. Since Git 2.38, git merge-tree --write-tree performs the merge in memory and leaves your working tree untouched. It exits with status 0 for a clean merge and 1 for conflicts. With --name-only it also lists the conflicted files.
git merge-tree --write-tree --name-only main agent/billingThe first line of output is the object ID of the resulting tree. On conflict, the conflicted file names follow it.
You can check every branch in a loop:
#!/usr/bin/env bash
set -u
for branch in agent/auth agent/billing agent/search; do
if git merge-tree --write-tree --name-only main "$branch" >/dev/null; then
echo "$branch: merges cleanly into main"
else
echo "$branch: conflicts with main"
fi
doneChecking against main only tells you about the first merge. After agent/auth is merged, agent/billing may conflict with it. To see this ahead of time, test the agent branches against each other:
git merge-tree --write-tree --name-only agent/auth agent/billing
git merge-tree --write-tree --name-only agent/auth agent/search
git merge-tree --write-tree --name-only agent/billing agent/searchThe result is a conflict map. It shows which pairs overlap and in which files. You can use it to choose a merge order and to decide which agent should rework its branch.
Choose a merge order
The first branch you merge never conflicts with anything new. Each later branch has to absorb everything merged before it. A few rules keep the work small:
- Merge branches that touch shared, central files first. Later branches then adapt to the new shape of those files.
- Merge small, low-risk branches early. They are quick to review and quick to roll back.
- If two branches overlap heavily, merge one, then ask the other agent to rework its branch on top of the result. Do not resolve the overlap yourself.
Update the branch in its own worktree
When a branch conflicts with main, resolve the conflict in the agent's worktree, not in the main checkout. The branch is checked out there, so Git does not let you switch to it anywhere else. The in-progress state of a merge or rebase also lives in that worktree. A half-finished resolution on agent/billing does not disturb main or any other agent.
You have two ways to bring main into the branch:
cd ../myrepo-billing
git rebase maincd ../myrepo-billing
git merge mainRebase replays the agent's commits on top of main. It gives a linear history, but you may resolve conflicts once per commit, and it rewrites the branch's commits. Merge resolves everything once and keeps the commits as they were. For agent branches that have not been shared with anyone, either works. Prefer merge when the agent made many small commits that touch the same lines, because it asks you to resolve those lines once.
Do this only while the agent is stopped. If an agent is still editing the worktree, a rebase will collide with its uncommitted changes.
Read the conflict
After a conflicted merge or rebase, list the files that need attention:
git status --short
git diff --name-only --diff-filter=UFiles marked UU have changes from both sides. Inside a file, the markers look like this:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> agent/billingThe top block comes from the side you are on. The bottom block comes from the side being applied. Edit the file to its correct final content and delete all three marker lines. Then stage it and continue:
git add src/config.py
git merge --continue # or: git rebase --continueWhich side is "ours"?
git checkout --ours and --theirs pick one whole side of a file, but their meaning depends on the operation:
| Operation | `--ours` is | `--theirs` is |
|---|---|---|
| `git merge agent/x` (run on `main`) | `main` | the agent branch |
| `git merge main` (run on the agent branch) | the agent branch | `main` |
| `git rebase main` (run on the agent branch) | `main` | the agent's commit being replayed |
The last row surprises people, because a rebase checks out main and replays the agent's work onto it. Check the table before you run git checkout --theirs. If you are unsure, open the file and read the markers.
Files you should regenerate, not merge
Lockfiles and other generated files conflict often when agents add dependencies. Merging them by hand is error-prone. Resolve the source file that declares the dependencies, then regenerate the lockfile:
git checkout --theirs package-lock.json # take main's copy during a rebase onto main
npm install
git add package.json package-lock.jsonThe same applies to compiled output, snapshots and generated clients. Resolve the inputs, run the generator, and stage the result. Better still, list generated files in your task notes as off-limits, so agents do not edit them directly.
Record resolutions with rerere
If you rebase and re-merge the same branches several times, you may resolve the same conflict repeatedly. Git can remember how you resolved it:
git config rerere.enabled trueThe setting is stored in the shared repository configuration, so it applies in every worktree. After you resolve a conflict once, Git reapplies that resolution when the same conflict appears again. It still marks the files as conflicted, and you still need to check and stage them.
Let an agent do the resolution, with limits
An agent can resolve conflicts well when the conflict is mechanical, since it can read both sides and the tests. Start it inside the branch's worktree and give it a specific job:
- Tell it which branch to update from, such as "merge
maininto this branch and resolve conflicts". - Tell it to keep the intent of both changes, and to stop and ask if the two changes contradict.
- Tell it to run the test suite before it commits.
Then review the merge commit the way you would review any agent work. For a merge commit, git show --cc HEAD displays only the lines where the result differs from both parents, which is where resolution mistakes tend to be.
Catch semantic conflicts with tests
A clean merge proves nothing about behavior. After every merge into main, run the full test suite before you merge the next branch:
git switch main
git merge --no-ff agent/auth
npm testIf the tests fail, find out which branch caused the failure. Because you merged one branch at a time, the cause is the merge you just did. Reset only that merge if it is still local and unpushed. A more cautious option is to fix the problem on the agent branch, in its worktree, and merge again.
Back out safely
An unfinished merge or rebase can always be abandoned without losing the agent's commits:
git merge --abort
git rebase --abortUse whichever matches the operation in progress. Either command returns the worktree to its state before you began, so you can step back and rethink the merge order.
Prevent the next conflict
Resolution is the expensive path. Most conflicts can be avoided when you plan the tasks:
- Assign each agent a set of files or directories to own, and write it in the task description.
- Have one agent make shared changes, such as renames, formatting or dependency upgrades, before the others start.
- Keep tasks small, so branches live for hours and not days. The longer a branch diverges from
main, the more it conflicts. - When two tasks must touch the same file, run them one after the other, or merge the first before starting the second from the new
main.
Exercise
You run git merge agent/billing on main and hit a conflict in src/pricing.py. You want to keep the whole agent/billing version of that file. Which checkout option do you use, and what must you do before finishing the merge?
Show answer
Run git checkout --theirs src/pricing.py, because in a merge run on main the agent branch is "theirs". Then run git add src/pricing.py to mark it resolved and git merge --continue to finish. Before that, check that you really want to drop all of main's changes to the file, since --theirs discards them, including the lines that did not conflict.
Exercise
git merge-tree --write-tree main agent/search exits with status 0, and you merge the branch. The tests then fail. What kind of conflict is this, and what is the likely cause?
Show answer
It is a semantic conflict. The two sets of changes touched different lines, so Git merged them cleanly, but they are incompatible in behavior. A typical cause is that one side renamed or changed the signature of a function that the other side calls or relies on.