Sub topic 11
Resolving conflicts between agent branches
This chapter assumes you can commit, branch, merge and diff in Git, and that you have already run two or more agents in separate worktrees. You should also know how to review an agent branch with git diff main...agent/auth.
Why conflicts still happen
Worktrees keep agents from overwriting each other's files while they work. They do not stop two branches from changing the same lines. Git only notices that when you merge the second branch.
Conflicts usually come from three places:
- Two tasks edit the same file, such as a router, a config file or a shared helper.
- Both agents add entries to the same list, such as imports, dependencies or exports.
- One agent renames or moves a file that another agent edits.
Good task splits make these rare. They are not always avoidable, so plan to handle them.
Merge one branch at a time
Merge the branches in sequence and run your tests after each merge. The first merge is always clean, because main has not moved. The second branch is the first to meet changes it never saw.
git switch main
git merge --no-ff agent/auth
git merge --no-ff agent/searchIf the second merge reports a conflict, Git stops and marks the files. List them with:
git status --shortFiles marked UU are conflicted on both sides. Files marked AA were added on both sides.
Read the markers
Inside a conflicted file, Git shows both versions:
<<<<<<< HEAD
from app.routes import auth_router
=======
from app.routes import search_router
>>>>>>> agent/searchThe section above ======= is what main has now, including the earlier merges. The section below is the incoming branch. In this example both lines are wanted, so the fix is to keep both and remove the markers. Choosing one side would silently delete an agent's work.
After editing, stage the file and finish the merge:
git add app/routes.py
git commitTo abandon a merge that has gone wrong, run git merge --abort. Nothing is lost, because the agent branch is untouched.
Let the agent rebase its own branch
Instead of resolving the conflict yourself, you can send the work back to the agent that wrote it. That agent knows its intent. Its worktree is still on disk, so you can ask it to bring its branch up to date with main:
cd ../myrepo-search
git rebase mainThe agent resolves each conflict, runs git rebase --continue, and runs its tests again. Afterwards the merge into main is clean.
Rebasing rewrites the agent's commits. That is safe here only because nobody else has based work on the branch. Do not rebase a branch that has been pushed and shared.
Check that the result works
A merge with no conflict markers can still be broken. Two agents might each change a function's callers in ways that compile alone but fail together. Run the full test suite after every merge, not only at the end.
Exercise
You merged agent/auth into main, and merging agent/search now stops with a conflict in app/routes.py. You decide not to resolve it now. Which command returns main to its state before the merge, and why is the agent's work safe?
Show answer
Run git merge --abort. It restores main and the working tree to the state before the merge started. The agent/search branch was only read during the merge, so its commits are unchanged and you can merge it again later, or have the agent rebase onto main first.
Exercise
A merge of agent/billing completes with no conflicts, but the tests fail. Name one reason this can happen and what you should do next.
Show answer
Git detects only overlapping text changes. Two branches can change related code in different places, for example a function signature in one and a new caller in another, so the combined result is wrong without any conflict. Run the tests after each merge to find which merge broke them. Then fix it on a new commit, or have the agent of the later branch adapt its work to the merged main.