Sub topic 13
Follow-up Branches After a Squash or Rebase Merge
This chapter assumes you can commit, branch, push and rebase in Git, and that you have read the earlier chapters on squash and rebase merges. It covers what happens to a branch that was started from another branch after that other branch lands in main.
Why the problem appears
Suppose you build feature-b on top of feature-a, because it needs code from feature-a. You open two pull requests. Then feature-a is merged into main.
With a merge commit, main contains the original commits of feature-a. Git sees that feature-b already includes them, so nothing is duplicated.
With squash and merge or rebase and merge, main gets new commits with new SHAs. Their changes match the old commits, but Git tracks commits by identity, not by content. feature-b still contains the old feature-a commits, and main contains different commits with the same changes.
What goes wrong
If you update feature-b with git rebase main or git merge main, Git tries to replay A1 and A2 on top of S. Those changes already exist in S, so you get conflicts in code you never touched in feature-b. The pull request for feature-b may also show the feature-a commits in its diff and commit list.
GitHub can reduce this in one case. If feature-b's pull request targets feature-a, GitHub retargets it to main when feature-a is merged and its branch is deleted. The retargeted pull request still carries the old commits unless you fix the branch yourself.
The fix: rebase --onto
Tell Git which commits belong only to feature-b. Those are the commits after the old tip of feature-a.
git fetch origin
git rebase --onto origin/main feature-a feature-bRead the command as: take the commits reachable from feature-b but not from feature-a, and replay them onto origin/main. Here feature-a must still point to the old tip. If you deleted or updated your local feature-a, find the old tip with git reflog or from the closed pull request, and use its SHA instead.
Then push the rewritten branch:
git push --force-with-lease origin feature-b--force-with-lease refuses to overwrite the remote branch if someone else pushed to it since your last fetch. That makes it safer than --force.
You can check the result before pushing:
git log --oneline origin/main..feature-bThis should list only the commits that belong to feature-b.
Ways to avoid it
- Merge the base pull request first, then branch from the updated
main, when the work allows it. - Keep branches small and independent, so few branches depend on each other.
- Use a merge commit for stacked work, where a chain of dependent branches is common.
- Run
rebase --ontoimmediately after each base merge, before conflicts pile up.
Summary of the options
| Situation | Result on `feature-b` | Action |
|---|---|---|
| `feature-a` merged with a merge commit | No duplicate commits | Merge or rebase `main` normally |
| `feature-a` squash-merged | Old commits duplicate the squash | `git rebase --onto origin/main feature-a feature-b` |
| `feature-a` rebase-merged | Old commits duplicate the new ones | `git rebase --onto origin/main feature-a feature-b` |
Exercise
You squash-merged feature-a into main. Your feature-b branch was started from feature-a, and git rebase main now reports conflicts in files that only feature-a changed. Why does this happen, and which command fixes it?
Show answer
The squash created a new commit on main with the same changes but a different SHA. feature-b still contains the original feature-a commits, so rebasing replays them on top of changes that already exist. Abort the rebase with git rebase --abort, then run git rebase --onto origin/main feature-a feature-b so only feature-b's own commits are replayed, and push with git push --force-with-lease origin feature-b.