Sub topic 13
Follow-up Branches After a Squash or Rebase Merge
This chapter assumes you can commit, branch, push, and pull. It also assumes you can read git log --graph output and have merged a pull request on GitHub. You should know that squash and rebase merges write new commits to the base branch. This chapter explains what that does to any branch that was built on top of the merged branch.
The problem in one picture
A common workflow is to start feature-b from feature-a before feature-a is reviewed. This is a stacked branch. feature-b needs the code from feature-a, so you branch from it and keep working.
Then feature-a is merged with squash and merge. The base branch main gets one new commit with the same file changes, but that commit has a different hash from every commit on feature-a. Git tracks history by commit hash, not by content. From Git's point of view, main does not contain the commits that feature-b was built on.
After the squash merge, main holds a new commit S that contains the changes of A1 and A2 combined.
The same thing happens with rebase and merge. Rebase and merge replays A1 and A2 onto main as new commits, call them A1' and A2'. Their content matches the originals, but their hashes differ.
Why it causes conflicts
Suppose you now update main and open a pull request from feature-b into main. Git compares feature-b against main. It finds the merge base, which is M1. Everything after M1 on feature-b counts as new: A1, A2, and B1.
Two things go wrong:
- The pull request shows A1 and A2 again, even though their changes are already in
mainthroughS. - Merging can produce conflicts. Both
Sand A1/A2 edit the same lines. Git sees two independent edits to the same place and asks you to resolve them, even though the result is identical.
Further changes to main make this worse. If someone edits a file that feature-a touched, the duplicate changes now conflict with real work.
A normal merge commit does not have this problem. With a merge commit, A1 and A2 become reachable from main, so feature-b shares real history with it.
Fix 1: rebase with `--onto`
The clean fix is to tell Git which commits belong to feature-b only, and to move just those. The command git rebase --onto takes three arguments: the new base, the old base (an exclusive starting point), and the branch to move.
git fetch origin
git rebase --onto origin/main feature-a feature-bRead it as: take the commits that are on feature-b but not on feature-a, and replay them on top of origin/main. Only B1 is replayed. A1 and A2 are left behind, because they are already in main as S.
Check the result:
git log --oneline --graph --decorate origin/main feature-bYou should see feature-b with a single commit on top of S. Since you rewrote the branch, you must push with a force. Use --force-with-lease, which refuses to overwrite commits you have not seen.
git push --force-with-lease origin feature-bThen change the pull request's base branch. If feature-b already has a pull request targeting feature-a, GitHub often retargets it to main automatically when feature-a is merged and its branch is deleted. Check the base branch shown on the pull request, and change it with the Edit button if needed.
Requirement: `feature-a` must still exist locally
The --onto form above uses the name feature-a as the old base. After the merge, your local feature-a still points to A2, so this works. If you deleted the local branch, or you pulled and moved it, use the commit hash of A2 instead. Find it with git log feature-b and look for the last commit that belongs to feature-a.
git rebase --onto origin/main <A2-sha> feature-bFix 2: a plain rebase and skipping duplicates
If you run a plain git rebase origin/main from feature-b, Git replays A1, A2, and B1. Git has a duplicate check that skips a commit whose change is identical to one already upstream (based on the patch ID). That check works for rebase and merge in many cases, since A1' has the same patch as A1. It usually does not work for squash, because S is a combination of two patches and has no matching single-commit patch ID.
So for a squash merge, a plain rebase often stops with conflicts on A1 and A2. You can recover:
- Run
git rebase --abortto return to the state before the rebase. - Use the
--ontocommand from Fix 1.
If you are already in the middle of a plain rebase, you can also skip each duplicate commit with git rebase --skip. Do this only after you check that the commit's changes are in main.
Fix 3: merge `main` into the branch
You can also merge main into feature-b and resolve the conflicts by hand. This avoids a force push. The cost is a merge commit on your branch and a manual resolution of duplicate changes. When resolving, keep the version from main, because it already includes the work of feature-a. It is easy to make a mistake here, so prefer the --onto rebase when you can.
Comparing the fixes
| Approach | Needs force push | Conflicts expected | Notes |
|---|---|---|---|
| `git rebase --onto origin/main feature-a feature-b` | Yes | Only real ones | Cleanest result. Needs the old base. |
| Plain `git rebase origin/main` | Yes | Often, after a squash | Duplicates are skipped for rebase merges but usually not for squash. |
| `git merge origin/main` | No | Often, after a squash | Adds a merge commit. You resolve duplicate edits by hand. |
Avoiding the problem
You can reduce the pain with a few habits:
- Merge
feature-afirst, then createfeature-bfrom the updatedmain. This is the simplest option when the work can wait. - Keep stacked branches small and short-lived, so the rebase has little to replay.
- Agree on a merge method for stacked work. If the team uses merge commits for stacks, the history stays connected and none of the fixes above are needed.
- Before merging the lower branch, note its last commit hash. You will need it for
--ontoif you delete the branch.
Rebase merge on the stack
Rebase and merge behaves slightly differently. The new commits A1' and A2' have the same content as A1 and A2, so Git's patch-ID check can often skip them during a plain rebase. This does not always hold. If GitHub resolved anything differently, or if the commits were reordered, the patches no longer match. The --onto command works in both cases, so use it as your default.
Checking that nothing was lost
After the rebase, confirm that the branch differs from main only by your own work:
git diff --stat origin/main feature-bThe output should list only files that feature-b changed. If it lists files from feature-a, the duplicate commits are still there.
Exercise
You branched feature-b from feature-a. feature-a was then merged with squash and merge, and its branch was deleted on GitHub. Your pull request for feature-b now shows the commits from feature-a and reports conflicts. Which command fixes this, and why does the problem happen?
Show answer
Run git fetch origin and then git rebase --onto origin/main feature-a feature-b (use the hash of the last feature-a commit if the local branch is gone), and push with git push --force-with-lease origin feature-b. The problem happens because the squash merge created a new commit on main with a different hash. The original feature-a commits are not reachable from main, so Git treats them as new work on feature-b and conflicts with the squashed copy of the same changes.
Exercise
Why does a merge commit not cause this problem for a stacked branch?
Show answer
A merge commit keeps the original commits of feature-a and makes them reachable from main. The merge base of feature-b and main then sits after those commits, so the pull request shows only the commits unique to feature-b.