Sub topic 10
Restacking after the bottom branch is squash-merged
This chapter assumes you have read the earlier chapters of this tutorial. You should be able to build a stack of branches, rebase it with git rebase --update-refs, and push it with --force-with-lease. You need Git 2.38 or newer.
The goal here is to handle one specific event. The bottom branch of your stack is squash-merged on the remote, and you must move the rest of the stack onto the updated main.
Why a plain rebase fails after a squash merge
Suppose your stack looks like this:
feature-1has commits A and B on top ofmain.feature-2adds commit C on top offeature-1.feature-3adds commit D on top offeature-2.
The pull request for feature-1 is squash-merged. The host builds one new commit, S, on main. S has the same file changes as A and B combined, but it is a different commit with a different ID. Git does not record that S replaces A and B.
Now you try the obvious command:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainA plain rebase replays every commit that is reachable from feature-3 but not from origin/main. A and B are not reachable from origin/main, because S is a different commit. Git therefore tries to replay A, B, C, and D on top of S.
Git can skip a commit whose patch is identical to one already upstream. It detects this with a patch ID, which is a hash of the diff. S is a combination of A and B, so its patch ID matches neither of them. Replaying A then tries to add changes that S already contains. That usually produces conflicts, or duplicate changes if the lines happen to merge cleanly.
You can run git rebase --abort to leave a rebase that has started going wrong.
The table shows how each merge strategy affects a plain rebase of the rest of the stack:
| Merge strategy on the remote | New commits on `main` | Plain `git rebase origin/main` on the next branch |
|---|---|---|
| Merge commit | Original commits plus a merge commit | Works. A and B are already reachable from `main`. |
| Rebase and merge | Copies of A and B with new IDs | Usually works. Git skips copies that have the same patch ID. |
| Squash and merge | One commit S replacing A and B | Fails or conflicts, because the history link is gone. |
Telling Git which commits to move
git rebase --onto lets you state the range of commits yourself. Its form is:
git rebase --onto <newbase> <upstream> [<branch>]Git replays the commits in <upstream>..<branch> on top of <newbase>. If you leave out <branch>, it uses the current branch. The commits that are reachable from <upstream> are the ones Git leaves out.
For this stack you choose:
<newbase>isorigin/main, which now contains S.<upstream>isfeature-1. Its commits A and B are already represented by S, so they must not be replayed.<branch>isfeature-3, the current branch.
The range feature-1..feature-3 contains only C and D. Those are the commits that still need a new home.
The diagram shows the stack before and after the restack. C and D are copied onto S, and A and B are left behind.
Restacking with --onto and --update-refs
Add --update-refs so that feature-2 moves as well as feature-3:
# feature-1 was squash-merged into main on the remote
git fetch origin
git switch feature-3
git rebase --update-refs --onto origin/main feature-1Git builds a todo list from feature-1..feature-3. It places an update-ref refs/heads/feature-2 line after C. When the rebase reaches that line, feature-2 is moved to the rewritten C. The branch feature-1 is not in the range, so it keeps pointing at B. That is what you want, because you are about to delete it.
A runnable practice repository
This script builds the situation above in a throwaway directory. It uses a local bare repository as the remote and simulates the squash merge with git merge --squash:
set -e
rm -rf /tmp/restack-demo
mkdir /tmp/restack-demo
cd /tmp/restack-demo
git init --bare -b main remote.git
git clone remote.git work
cd work
git config user.name "Demo"
git config user.email "[email protected]"
echo base > base.txt
git add base.txt
git commit -m "Base"
git push origin main
git switch -c feature-1
echo a > a.txt
git add a.txt
git commit -m "A: add a.txt"
echo b >> a.txt
git commit -am "B: extend a.txt"
git switch -c feature-2
echo c > c.txt
git add c.txt
git commit -m "C: add c.txt"
git switch -c feature-3
echo d > d.txt
git add d.txt
git commit -m "D: add d.txt"
# Simulate the host squash-merging feature-1
git switch main
git merge --squash feature-1
git commit -m "Add a.txt (squash of feature-1)"
git push origin main
# Restack
git switch feature-3
git rebase --update-refs --onto origin/main feature-1
git log --oneline --decorate --graph origin/main..feature-3The final log shows two commits, D and C, sitting on the squash commit. The names feature-3 and feature-2 are attached to them. Before you run the last rebase, you can try git rebase origin/main instead. It stops with an add/add conflict on a.txt, because replaying A tries to create a file that S already created.
Checking the result before you push
A restack rewrites commits, so check that it carried over what you meant. git range-diff compares two ranges commit by commit. After a rebase, ORIG_HEAD points at the old tip of the branch:
git range-diff feature-1..ORIG_HEAD origin/main..feature-3Each old commit should appear paired with its rewritten copy. Commits marked = are unchanged in content, and ! means the patch differs. Because the base changed, ! can be legitimate. It points out where to look if you resolved conflicts.
Pushing and cleaning up
feature-2 and feature-3 have new commit IDs, so the remote refuses a normal push. Use --force-with-lease, as in the earlier chapter, and name only the branches that changed:
git push --force-with-lease origin feature-2 feature-3Do not push feature-1. It is finished. Then clean up:
- Retarget the pull request for
feature-2so its base ismain. Many hosts do this for you when the merged branch is deleted. Check on yours, because a pull request that still points at a deleted base cannot show a sensible diff. - Delete the remote
feature-1branch if the host did not do it:git push origin --delete feature-1. - Delete the local branch with
git branch -D feature-1. The lowercase-drefuses, because Git cannot see that S contains the work offeature-1. Make sure the squash merge really happened before you use-D.
Repeating the process
When feature-2 is squash-merged, you do the same thing one level up. The new upstream is feature-2:
git fetch origin
git switch feature-3
git rebase --update-refs --onto origin/main feature-2
git push --force-with-lease origin feature-3Here there are no intermediate branches left, so --update-refs has nothing to move. Leaving it in the command is harmless, and it keeps the command the same shape every time. The routine is: fetch, name the merged branch as the upstream, rebase from the top of the stack, and push the survivors.
Edge cases
- Reviewers changed the bottom branch. If
feature-1was amended during review and then squash-merged, S contains those changes, but yourfeature-2commits were written against the old B. Replaying C may conflict where S changed the same lines. Resolve those conflicts as in any rebase.git rebase --continuecarries on to theupdate-reflines afterward. - Squash of a single commit. If
feature-1held one commit and the host squashed it without editing, S may have the same patch ID as A. A plain rebase can then work, because Git skips A. Do not rely on this. The--ontoform is correct in both cases. - Another squash on a different branch. If the merged branch was in the middle of the stack, for example
feature-2, thenfeature-1must be merged first, or the lower branches must be rebased ontomainbefore you restack the top. Squash-merge from the bottom up. - Wrong range. If the rebase reports an unexpected number of commits in its todo list, run
git rebase --abortand checkgit log --oneline feature-1..feature-3before you try again. That command prints exactly the commits the rebase will replay.
Exercise
Your stack is main, feature-1, feature-2, feature-3. The pull request for feature-1 was squash-merged and you have run git fetch origin. Write the commands to move feature-2 and feature-3 onto the updated main, push them safely, and remove the merged local branch. Explain why git rebase origin/main is the wrong rebase command here.
Show answer
Run git switch feature-3, then git rebase --update-refs --onto origin/main feature-1, then git push --force-with-lease origin feature-2 feature-3, then git branch -D feature-1. A plain git rebase origin/main would also replay feature-1's commits A and B. The squash commit S contains their changes under a different commit ID, and S matches neither A nor B by patch ID, so Git does not skip them and the rebase conflicts or duplicates changes. With --onto and feature-1 as the upstream, only the commits in feature-1..feature-3 are replayed.
Exercise
After a restack, how can you check that the rebased commits still match the originals, and which ref holds the original tip of feature-3?
Show answer
Run git range-diff feature-1..ORIG_HEAD origin/main..feature-3. ORIG_HEAD holds the old tip of feature-3 right after the rebase. Each old commit is paired with its rewritten copy, and any ! marks a commit whose patch changed and should be reviewed.