Sub topic 10
Reviewing and Merging Each Agent's Branch
Your agents have finished. Each one worked in its own worktree and committed to its own agent/ branch. This chapter covers how to review those branches and bring them into main one at a time.
This chapter assumes you are comfortable with git diff, git log and git merge. It also assumes you have already created one worktree and one branch per agent, as in the earlier chapters. The examples use the branches agent/auth, agent/billing and agent/search, all created from main.
Why review is a separate step
An agent's branch is a proposal. The agent has not proven it correct, only produced it. Three things make review more important with parallel agents than with a single one:
- You did not watch the work happen. Several agents ran at once, so you have only the result.
- Each branch is correct only against the
mainit started from. Other branches may have landed since. - Agents sometimes change files outside the task, reformat code, or leave debug output behind.
So the rule is: review each branch against main, merge one at a time, and re-test after every merge.
Step 1: Check that the work is committed
A branch only contains what was committed. Anything an agent left uncommitted exists only in its worktree directory. Check each worktree before you review:
git -C ../myrepo-auth status --short
git -C ../myrepo-auth log --oneline main..HEADThe first command lists uncommitted and untracked files. The second lists commits on the branch that are not on main. If the second list is empty, the agent made no commits, and git diff main...agent/auth will show nothing even though files changed on disk.
You can commit the leftovers yourself after looking at them:
git -C ../myrepo-auth add -A
git -C ../myrepo-auth commit -m "auth: finish login token refresh"Be careful with add -A. It stages untracked files such as .env, logs and build output unless .gitignore excludes them. Read git status first.
Step 2: Read the diff against main
Use the three-dot form to see only what the branch changed:
git diff --stat main...agent/auth
git diff main...agent/authmain...agent/auth compares the branch tip with the point where it diverged from main. The two-dot form main..agent/auth compares the two tips directly. If main has moved on since the branch started, the two-dot diff also shows the reverse of every new main change, which looks like the agent deleted code it never touched.
| Form | Compares | Use it to |
|---|---|---|
| `main...agent/auth` | Branch tip against the merge base | See only what the agent changed |
| `main..agent/auth` | Branch tip against `main` tip | See the exact result of a merge if `main` has not moved |
Start with --stat. It answers a quick scope question: did the agent stay inside the files its task covered? A task about authentication that touches billing/invoice.py deserves a closer look.
Then read the full diff with these checks in mind:
- Does every change belong to the task?
- Are there unrelated reformatting changes that will cause conflicts later?
- Did the agent edit tests to make them pass, instead of fixing the code?
- Are there leftover debug prints, commented-out code or hard-coded values?
- Did it add or change dependencies, lock files, or configuration?
To list commits with their changed files:
git log --stat --oneline main..agent/authReading commit by commit is useful when a branch has many small commits and the final diff is large.
Run the tests where the work lives
You do not need to switch branches to test. The branch is already checked out in its worktree, so run the suite there:
cd ../myrepo-auth
npm testUse your project's own test command. This tests the branch as the agent left it, with its own dependencies and its own port settings.
Step 3: Merge one branch at a time
Switch to the primary worktree and merge:
cd ../myrepo
git switch main
git merge --no-ff agent/auth--no-ff forces a merge commit even when a fast-forward is possible. The merge commit records that these commits came from one unit of agent work, and it gives you a single commit to revert if the work turns out to be bad:
git revert -m 1 <merge-commit>Here -m 1 tells Git to treat the first parent, which is main, as the mainline.
If you prefer a linear history, you can squash instead:
git merge --squash agent/auth
git commit -m "Add token refresh to auth"Squashing discards the individual agent commits and does not record a merge relationship. Because of that, git branch -d agent/auth will later refuse to delete the branch, claiming it is not merged. You must use -D, so confirm the content really landed first.
Choosing a merge strategy
| Strategy | History | Revert | Trade-off |
|---|---|---|---|
| `merge --no-ff` | Keeps agent commits plus a merge commit | One `revert -m 1` | Noisier history |
| `merge --squash` | One commit per agent task | One `revert` | Loses detail, needs `branch -D` |
| `rebase` then fast-forward | Linear, keeps commits | Revert each commit | Rewrites the agent branch |
Rebasing an agent branch is safe only when nothing else uses it. In this workflow only one worktree has the branch checked out, so that is usually true. Stop the agent first, though. A running agent that commits while you rebase will produce a confusing state.
Step 4: Update the remaining branches
After the first merge, main has changed. The other branches were built against the old main. Before merging the next one, bring main into it and test the combination:
cd ../myrepo-billing
git merge main
npm testThis is where interactions show up. Two branches can each pass their tests alone and still break each other, for example when one renames a function the other calls. Textual conflicts are the visible case. Behavioral conflicts, where the merge is clean but the code is wrong, only appear when you run the tests after combining.
If the merge produces conflicts, resolve them in the agent's worktree, not on main. That keeps main clean until the branch is ready. Conflict resolution is covered in its own chapter.
Choosing the merge order
Order matters when branches overlap. A practical approach:
- Merge the branch with the smallest, most self-contained diff first. It is the least likely to cause conflicts.
- Merge branches that change shared files, such as configuration, schemas or lock files, early, so later branches adapt to them.
- Leave the broadest branch, such as a rename across many files, for last or first by itself, because it conflicts with almost everything.
A quick way to see which branches overlap before you start is to compare the file lists:
git diff --name-only main...agent/auth | sort > /tmp/auth.files
git diff --name-only main...agent/billing | sort > /tmp/billing.files
comm -12 /tmp/auth.files /tmp/billing.filescomm -12 prints only the lines present in both files, which are the files both branches modified. An empty result means a textual conflict is unlikely.
Common errors
- Reviewing with
git diff main agent/authaftermainmoved. This shows reversed changes. Use the three-dot form. - Empty diff for a branch that clearly changed files. The agent did not commit. Check
git statusin its worktree. git branch -dsays the branch is not fully merged after a squash. This is expected. Verify the content, then use-D.- Testing only on the agent branch. The branch passes against an old
main. Re-test after mergingmaininto it, and again after merging intomain. - Committing secrets.
git add -Ain a worktree where.envwas copied in can stage it. Keep.envin.gitignore.
Exercise
You merged agent/auth into main. Now you run git diff main..agent/billing and see that it deletes a function you know the billing agent never touched. What happened, which command should you have used, and what do you do before merging agent/billing?
Show answer
main gained the auth changes after agent/billing branched, and the two-dot diff compares the two tips, so it shows those new main changes in reverse as if the billing agent removed them. Use git diff main...agent/billing to see only the billing agent's changes. Before merging, run git merge main inside the billing worktree, resolve any conflicts there, and run the tests on the combined result.
Exercise
An agent says it finished, but git diff main...agent/search prints nothing. Name two commands that find the cause and say what you will probably find.
Show answer
Run git -C ../myrepo-search status --short to list uncommitted or untracked files, and git -C ../myrepo-search log --oneline main..HEAD to list commits not on main. You will probably find that the agent edited files but never committed them, so the branch has no new commits. Review the leftovers, then commit them on the branch.