Sub topic 10
Restacking after the bottom branch is squash-merged
This chapter assumes you can run git rebase on one branch and know how --update-refs moves every branch in a stack (Git 2.38 or newer). It also assumes you know how remote branches and pull requests work. The chapter covers one situation: the bottom branch of your stack was squash-merged, and you need to move the rest of the stack onto the new main.
Why a plain rebase goes wrong
Suppose your stack is main, feature-1, feature-2 and feature-3. The pull request for feature-1 is squash-merged on the remote. The squash creates one new commit on main that has the same changes as all the feature-1 commits, but a different commit ID.
Your feature-2 and feature-3 branches still contain the original feature-1 commits. Git compares commits by ID, so it does not know that the new squash commit and those old commits are the same work.
If you run git rebase origin/main from feature-3, Git tries to replay the old feature-1 commits on top of the squash commit. The changes are already in main, so you get duplicate work or conflicts in code you did not touch.
Replay only the commits after feature-1
git rebase --onto solves this. It takes three pieces of information: the new base, the old base, and the branch to move.
# 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-1Read the command as follows:
--onto origin/mainis where the replayed commits will start.feature-1is the old base. Git replays the commits infeature-1..feature-3, which are the commits reachable fromfeature-3but not fromfeature-1.--update-refsmovesfeature-2to its rewritten commit whilefeature-3is rebased.
The old feature-1 commits are excluded from the replay, so the squash commit takes their place. After the command, feature-2 and feature-3 sit directly on origin/main.
Check the result:
git log --oneline --decorate --graph origin/main..feature-3You should see only the commits of feature-2 and feature-3, with both branch names shown.
Finish the cleanup
Three follow-up steps complete the restack:
- Push the rewritten branches. Their commit IDs changed, so use
git push --force-with-lease origin feature-2 feature-3. - Retarget the pull request for
feature-2so its base ismaininstead offeature-1. Some hosting services do this automatically when the base branch is deleted. - Delete the local
feature-1branch withgit branch -D feature-1. Git may refuse the lowercase-dbecause the squash commit is not an ancestor offeature-1. Use-Donly after you have confirmed the pull request was merged.
When the old base is not a branch name
If you already deleted feature-1, you can pass the commit ID of its tip instead. Find it in the reflog:
git reflog show feature-1Then use that ID in place of the branch name:
git rebase --update-refs --onto origin/main 1a2b3c4The behavior is the same, because Git needs only a commit to exclude from the replay.
Check yourself
Exercise
Your stack is main, feature-1, feature-2 and feature-3. feature-1 was squash-merged on the remote. Write the command, run from feature-3, that moves feature-2 and feature-3 onto the updated main without replaying the old feature-1 commits. Then explain why git rebase origin/main is not enough.
Show answer
git fetch origin, then git rebase --update-refs --onto origin/main feature-1. A plain git rebase origin/main tries to replay the old feature-1 commits, because their IDs differ from the squash commit's ID. That produces duplicate changes or conflicts. With --onto origin/main feature-1, Git replays only the commits in feature-1..feature-3, and --update-refs moves feature-2 along with feature-3.
Exercise
After the restack, which command pushes the rewritten branches, and why is --force-with-lease preferred over --force?
Show answer
git push --force-with-lease origin feature-2 feature-3. The commit IDs changed, so a normal push is rejected. --force-with-lease also refuses to overwrite a remote branch that moved since your last fetch, so you do not destroy a teammate's commits.