Sub topic 10
Reviewing and Merging Each Agent's Branch
This chapter assumes you can already create worktrees, run an agent in each one, and use everyday Git commands such as diff, log, and merge. Each agent has finished its task on its own agent/ branch. Your job now is to decide what goes into main.
Why review comes before merging
An agent's branch is a proposal. The agent may have changed files outside its task, left debug code, or skipped tests. Reviewing each branch on its own, before it touches main, keeps one bad branch from blocking the others.
Check what the branch changed
Use the three-dot diff to see only what the agent branch added since it diverged from main. Because the branches share one repository, you can run this from any worktree.
git diff main...agent/authTwo dots (main..agent/auth) compare the two branch tips directly. If main has moved since the branch was created, the two-dot form also shows main's newer changes as if the agent had removed them. The three-dot form avoids that.
Check the scope first, then the details:
git diff --stat main...agent/auth
git log --oneline main..agent/authThe --stat output lists files and line counts. Look for files outside the task. An auth task that edits billing code is a warning sign, and it often predicts conflicts later.
Run the tests in the agent's worktree
The worktree already holds the agent's files, so test there instead of checking the branch out again.
cd ../myrepo-auth
git status --shortRun your normal test command in that directory. Anything left over from git status, such as untracked files, is work the agent did not commit. Commit it or discard it deliberately.
Merge one branch at a time
Merge from main, which must be checked out in your original worktree. Using --no-ff creates a merge commit, so the branch's history stays visible as a unit and is easy to revert.
git switch main
git merge --no-ff agent/authAfter each merge, run the tests again on main. Do this before merging the next branch. Two branches can each pass alone and still fail together, for example when one renames a function the other calls. Merging one at a time tells you which branch caused the failure.
| Approach | History | Easy to revert as a unit | Use when |
|---|---|---|---|
| `merge --no-ff` | Keeps every commit plus a merge commit | Yes, revert the merge commit | You want the agent's steps preserved |
| `merge --squash` | One new commit, no link to the branch | Yes, one commit | The agent's commits are noisy |
With --squash, Git stages the changes but does not commit them. You must run git commit yourself, and git branch -d will later refuse to delete the branch because Git does not see it as merged. Use git branch -D only after you have confirmed the content is in main.
Choose a merge order
Merge the branch with the smallest, most self-contained change first. Later branches then rebase or merge onto a main that already contains the earlier work. If a later branch needs updating, do it in that agent's worktree:
cd ../myrepo-billing
git merge mainResolving conflicts is covered in the next chapter.
Exercise
You run git diff main...agent/search and see a change to a file the search task never mentioned. main has also received two new commits since the branch was created. Why use three dots here instead of two, and what should you do about the unexpected file?
Show answer
Three dots compare the branch with its merge base, so only the agent's own changes appear. Two dots would also show the two new main commits as reversed changes, which hides what the agent really did. For the unexpected file, check whether the edit is needed for the task. If it is not, revert that file on the agent's branch before merging, because out-of-scope edits are a common source of conflicts with other branches.