Sub topic 9
Restacking a Branch After a Squash Merge of Its Parent
This chapter assumes you can commit, branch, switch, and read git log. It also assumes you have read the earlier chapters on the upstream..branch range and the three-argument git rebase --onto <newbase> <upstream> <branch> form. Here you apply that form to one frequent situation: a branch built on top of another branch, where the lower branch was squash-merged.
The problem: a stacked branch with a vanished parent
A stacked branch is a branch started from another feature branch instead of from main. Suppose you have this history:
feature-ahas two commits, A1 and A2, started frommain.feature-bwas started from the tip offeature-aand has two commits, B1 and B2.
You open a pull request for feature-a, and your team merges it with a squash merge. A squash merge combines all the branch's changes into one new commit on main. It does not add A1 and A2 to main. It adds a new commit, which this chapter calls S, with different identity and a different hash.
main now contains the content of A1 and A2, but Git has no record of that. The graph does not link S to A1 or A2. Git decides what is already merged by reachability in the graph, so feature-b still looks like it carries four commits that main lacks.
Why a plain rebase goes wrong
Running git rebase main on feature-b uses main as both the new base and the upstream. Git finds the merge base of main and feature-b. That is M1, which is the point where feature-a left main. It then replays everything after M1 on feature-b: A1, A2, B1, and B2.
Replaying A1 and A2 is wrong, because their changes are already in main inside S. Depending on the content, you can get any of these results:
- Conflicts on lines that A1 or A2 changed and that S also contains.
- Commits that become empty and are dropped or stop the rebase.
- Duplicate changes if later edits shifted the surrounding code.
Git does skip a commit when another commit with an identical patch is already upstream. A squash commit combines several patches, so no single original commit matches it.
The fix: use the old parent as the upstream
You need to tell Git that the commits to move are only B1 and B2. They are exactly the commits reachable from feature-b but not from feature-a. That is the range feature-a..feature-b, so feature-a is the upstream argument:
git fetch origin
git rebase --onto origin/main feature-a feature-bRead the command as three parts:
- New base:
origin/main, which now includes S. - Upstream:
feature-a, the old parent. Everything reachable from it stays behind. - Branch:
feature-b, the branch to move.
Git computes feature-a..feature-b, which is B1 and B2. It checks out origin/main, copies those two commits on top as B1' and B2', and moves feature-b to B2'. The original A1 and A2 are never replayed.
After the rebase, the history looks like this:
# M1---S---B1'---B2' feature-b
# |
# main
#
# M1---A1---A2 feature-a (old, still present locally)A runnable demonstration
This script builds the scenario in a throwaway directory so you can watch the range and the result. It needs Git 2.28 or later for git init -b.
set -e
rm -rf /tmp/squash-demo && mkdir /tmp/squash-demo && cd /tmp/squash-demo
git init -q -b main
git config user.name demo
git config user.email [email protected]
echo base > base.txt && git add . && git commit -qm "M1: base"
git switch -q -c feature-a
echo a1 > a1.txt && git add . && git commit -qm "A1"
echo a2 > a2.txt && git add . && git commit -qm "A2"
git switch -q -c feature-b
echo b1 > b1.txt && git add . && git commit -qm "B1"
echo b2 > b2.txt && git add . && git commit -qm "B2"
git switch -q main
git merge --squash feature-a
git commit -qm "S: add feature A (squashed)"
echo "--- commits that --onto will replay:"
git log --oneline feature-a..feature-b
git rebase --onto main feature-a feature-b
echo "--- history after the rebase:"
git log --oneline --graph main feature-bThe first git log prints only B2 and B1. After the rebase, feature-b has M1, S, B1', and B2' in a straight line. The files a1.txt, a2.txt, b1.txt, and b2.txt all exist on feature-b, because S supplies the first two.
A checklist for restacking
Work through these steps in order. They apply the preview and verify habit from earlier in the tutorial.
- Run
git fetch originsoorigin/maincontains the squash commit. - Make sure
feature-astill points at the commit yourfeature-bwas built on. Rungit log --oneline feature-a..feature-band confirm it lists only the commits that belong tofeature-b. - Run
git rebase --onto origin/main feature-a feature-b. - Compare the result with
main. The commandgit diff --stat origin/main feature-bshould show only the changes thatfeature-bis supposed to add. - Push with
--force-with-lease(covered below).
If the preview in step 2 lists commits that belong to feature-a, stop. The upstream you chose is wrong, and rebasing would carry those commits along.
When feature-a is gone
Many workflows delete the branch after the merge. Deleting a branch only removes the pointer. The commits remain until garbage collection, and you can still name them. You have several options for the upstream:
| Situation | What to use as upstream |
|---|---|
| Local `feature-a` still exists | `feature-a` |
| Only `origin/feature-a` still exists | `origin/feature-a`, if it still points at the old tip |
| Branch deleted everywhere | The commit hash of A2, or `feature-b~2` (counting back over B2 and B1) |
| You want protection before cleaning up | Create a tag first: `git tag feature-a-old feature-a` |
Counting back from feature-b is the most direct approach when you know how many commits are your own:
git rebase --onto origin/main feature-b~2 feature-bHere feature-b~2 is A2, so the range feature-b~2..feature-b is B1 and B2. If you are unsure of the count, run git log --oneline on feature-b and find the last commit that belonged to feature-a. You can also find the old tip with git reflog if a branch was removed.
Conflicts during the restack
The range is correct, but conflicts can still happen. A typical cause: the reviewer asked for changes on feature-a that you made after you started feature-b, or other work landed on main that touches the same lines as B1 or B2. Git then pauses on the first conflicting commit.
Resolve it the same way as in any rebase:
git status
# edit the conflicted files and remove the conflict markers
git add path/to/file
git rebase --continueIf a commit became empty because main already has its change, use git rebase --skip. To go back to the state before the command, use git rebase --abort.
One conflict pattern is specific to squash merges. Suppose feature-a was edited after feature-b branched, and the squash includes those late edits. Then B1 and B2 were written against the old code, while main has the newer version. Conflicts in files that feature-a touched are expected. Resolve them against the version in main, since that is what the team accepted.
Pushing the result
The rebase created new commits B1' and B2'. If feature-b was already pushed, the remote still has the old B1 and B2, and a normal push is rejected. Use a lease-protected force push:
git push --force-with-lease origin feature-b--force-with-lease refuses to overwrite the remote branch if it holds commits you have not fetched, for example a teammate's push. Plain --force would discard them.
Then retarget the pull request. If your pull request for feature-b was opened against feature-a, change its base to main in your hosting service. Some services do this automatically when the parent branch is merged and deleted. Check that the diff of the pull request shows only B1 and B2.
Recovery if the range was wrong
If you rebased with the wrong upstream, the old feature-b is not lost. Right after the rebase, ORIG_HEAD points at the old tip, so this restores it:
git reset --hard ORIG_HEADIf other commands have overwritten ORIG_HEAD, use git reflog and reset to feature-b@{1} or whichever entry shows the old tip. Do this before pushing, since a force push publishes the mistake.
Summary of the idea
The squash merge breaks the link between the old branch and main. The --onto form replaces that link by naming the old parent as the boundary of the range. In the three arguments, the new base is where the commits go, the upstream is the old parent that bounds the range, and the branch is the one to move.
Exercise
Your branch feature-b has three commits of its own (B1, B2, B3) on top of feature-a. feature-a was squash-merged into main, and both the local and remote feature-a branches were deleted. You have fetched origin/main. Write the command that moves only B1, B2, and B3 onto origin/main, and give the command you run first to check the range.
Show answer
Run git log --oneline feature-b~3..feature-b first. It should list exactly B1, B2, and B3. Then run git rebase --onto origin/main feature-b~3 feature-b. Here feature-b~3 is the last commit that came from feature-a, so it works as the upstream even though the branch name is gone.
Exercise
After the restack, git diff --stat origin/main feature-b also lists files that only feature-a changed. What most likely went wrong, and how do you undo it?
Show answer
The upstream was wrong or feature-a had moved, so the replayed range included commits from feature-a. Undo the rebase with git reset --hard ORIG_HEAD (or the reflog entry feature-b@{1}), preview the range with git log --oneline <upstream>..feature-b until it lists only your own commits, and rerun the rebase with the correct upstream.