Topic 4 of 5 · 5 min read
Merging parallel agent branches
Goal: Merge branches from parallel agents in a safe order and catch integration breaks that each branch passed alone.
Merge one branch at a time, in a planned order, and run the full tests after every merge, because two branches that each pass alone can break when combined.
Check yourself · 1 of 3
Two agent branches merge with no text conflicts. What does that prove?
Why it matters
Each agent tested its own branch against the old main. Neither saw the other's changes. A clean text merge only means the edits did not overlap in the same lines. It says nothing about whether the code still works. Integration problems show up at the seams: a renamed function that another branch still calls, a changed return value, a migration that assumes the old schema.
How to do it
- Choose the order. Merge shared foundations first (contracts, schema, utilities), then features that depend on them, then the independent ones. Put the smallest, safest branch early to shrink drift for the others.
- Merge one branch. Update the branch from the new main before merging, or merge main into it, so conflicts show up in a branch you can still fix.
- Resolve conflicts by intent, not by taking one side. For each conflict ask what each branch was trying to do, and write code that does both. An agent can help, but you read the result.
- Run the full test suite on the merged result, not only the new branch's tests. Also build and typecheck.
- Review the combined diff, the change from the old main to the new main, not just each branch's diff. This is where interactions appear.
- Only then merge the next branch, and repeat steps 2 to 5.
Before and after
Weak: merge all three branches in a row, run the tests once at the end, and find 11 failures with no clue which merge caused them.
Fixed: test after each merge. The failure appears right after the second merge, so you know exactly which pair interacts. Here is a break that merged without any text conflict:
--- a/src/orders/format.ts
+++ b/src/orders/format.ts
@@ -4,3 +4,3 @@
-export function formatTotal(order: Order): string {
- return `$${order.total}`;
+export function formatTotal(order: Order, currency: string): string {
+ return `${currency}${order.total}`;
}
The branch that changed this signature passed its tests. The export branch, merged later, still calls formatTotal(order) and now prints "undefined12.50". Git saw no conflict because the two edits were in different files. Only the post-merge test run catches it.
What goes wrong
- Trusting a green branch. A passing branch was tested against an older main.
- Resolving a conflict by picking one side. The other branch's feature silently disappears.
- Testing once at the end. When several things break, you cannot tell which merge caused which.
- Reviewing only each branch's own diff. The bug lives in the combination.
Examples
The signature change
Branch A adds a currency argument to formatTotal. Branch B, written at the same time, adds a new call to formatTotal(order). The text merges cleanly. After A is merged, a typecheck on B flags the missing argument. Running the checks after each merge finds this at once.
Resolving by taking one side
Two branches both edited the same settings file. The quick fix keeps the incoming version and drops the other's new option. Its feature now ignores its own setting. Reading both intents and keeping both changes avoids the loss.
Key points
- A clean text merge only means no lines overlapped; it does not mean the combined code works.
- Merge foundations and contracts first, then dependent features, then independent ones.
- Merge one branch at a time, and run the full tests, build and typecheck after each one.
- Resolve conflicts by what each branch intended, so neither feature is lost.
- Review the combined diff from the old main to the new main, not only each branch alone.
Common mistakes
- Merging all branches first and running the tests at the end.
- Resolving conflicts by keeping one side without reading the other branch's intent.
- Skipping the build and typecheck because the unit tests passed.
- Reviewing each branch's diff on its own and never the combined result.
Check yourself · 2 of 3
You have three agent branches ready. Which approach catches integration bugs soonest?
Check yourself · 3 of 3
A merge conflict appears in a file both branches changed. What is the best way to resolve it?
Practice items, not counted towards your mastery.
Start here
- git-worktree documentation (opens in a new tab)DocsFree
Official reference for add, list, remove and prune: one repository, several working folders.
- Common workflows (Claude Code docs) (opens in a new tab)DocsFree
A vendor's own section on running parallel sessions with worktrees.
- Embracing the parallel coding agent lifestyle (Simon Willison) (opens in a new tab)ArticleFree
A practitioner's account of why review speed, not agent speed, is the limit.
More for this topic
- git-merge documentation (opens in a new tab)DocsFree
How merges, conflicts and aborting a merge work.
- Patterns for Managing Source Code Branches (Martin Fowler) (opens in a new tab)ArticleFree
Names the integration patterns and shows why long-lived branches make merges painful.
- Small CLs (Google Engineering Practices) (opens in a new tab)ArticleFree
Why small, self-contained changes are reviewed faster and merge with fewer surprises.