Skip to main content

Rebase a Stacked Branch After a Squash Merge

Use git rebase --onto main <old-parent> <branch> so only the branch's own commits move onto the squashed result.

The Fix​

Say part-2 was built on part-1, and part-1 was squash merged into main:

git fetch origin
git rebase --onto origin/main part-1 part-2

This takes the commits on part-2 that are not on part-1 and replays them on origin/main. Then force-push part-2 with --force-with-lease.

Why It Happens​

A squash merge writes one new commit to main. The original part-1 commits are not in main's history, so a plain git rebase main tries to replay them again. Git skips a commit whose change already exists, but part-1's commits rarely match the squashed result exactly once review fixes are in. The usual outcome is a set of conflicts against code that is already on main.

How Treq Handles It​

When a workspace's pull request merges on GitHub, Treq moves the workspace above it onto the merged workspace's target. The next layer is rebased onto main with only its own commits, without a manual --onto. See Stacks and Rebasing.