Sub topic 9
Restacking a branch after a squash merge of its parent
This chapter assumes you know how to commit, branch, and read git log. It also assumes you know that git rebase --onto <newbase> <upstream> <branch> replays the commits in upstream..branch onto newbase.
The problem: stacked branches
A stacked branch is a branch started from another feature branch instead of from main. Here feature-b was started from feature-a, because it needs code that feature-a adds.
o---o---o main
\
A1---A2 feature-a
\
B1---B2 feature-bNow feature-a is reviewed and merged into main with a squash merge. A squash merge combines all the branch's changes into one new commit on main. That commit has a new ID and no link to A1 or A2.
o---o---o---S main (S has the same changes as A1+A2)
\
A1---A2 feature-a
\
B1---B2 feature-bfeature-b still contains A1 and A2. Git cannot tell that S already holds their changes.
Why a plain rebase fails here
If you run git rebase main on feature-b, Git replays everything reachable from feature-b but not from main. That range includes A1, A2, B1, and B2. Git skips a commit only when main already contains a commit with an identical patch. S is a different patch, so A1 and A2 are replayed. They conflict with S, because S already made the same changes.
The fix: use the old parent as the upstream
The upstream argument bounds the range. If you name feature-a as the upstream, the range is feature-a..feature-b. That range holds only B1 and B2.
git fetch origin
git rebase --onto origin/main feature-a feature-bThe three arguments mean this:
| Argument | Value | Role |
|---|---|---|
| newbase | `origin/main` | Where the commits are placed. It contains `S`. |
| upstream | `feature-a` | The old parent. Commits reachable from it are excluded. |
| branch | `feature-b` | The branch to move. |
After the rebase, the history looks like this:
o---o---o---S---B1'---B2' feature-b
|
mainfeature-a still points at the old A2. You can delete it once the pull request is merged.
Check the range first
Preview the commits that --onto will replay. This catches a wrong upstream before any history changes.
git log --oneline feature-a..feature-bYou should see only B1 and B2. If you see commits from feature-a, you named the wrong upstream, or feature-a has moved since you branched.
Afterwards, check the result:
git log --oneline --graph --decorate origin/main feature-b
git diff origin/main feature-b --statThe diff should list only the files that B1 and B2 changed.
Conflicts and the finish
B1 and B2 may still conflict with changes that landed on main after S. Resolve each stop in the usual way:
git status
# edit the conflicted files
git add path/to/file
git rebase --continueUse git rebase --abort to return to the state before the rebase.
If feature-b was already pushed, the rebase has rewritten its commits. Push it with a lease:
git push --force-with-lease origin feature-bCommon errors
- Using
mainas the upstream. This replaysA1andA2, which produces conflicts withS. - Forgetting
git fetch. Without it,origin/maindoes not containS, and the new base is out of date. - Deleting
feature-abefore rebasing. Use the reflog to recover its old tip, or run the rebase first.
Exercise
feature-c was started from feature-b, and feature-b was squash-merged into main. Write the command that moves feature-c onto origin/main, and the command that previews the commits it will replay.
Show answer
Preview with git log --oneline feature-b..feature-c. Then run git rebase --onto origin/main feature-b feature-c. The upstream is feature-b because its original commits are the ones the squash commit already replaced.
Exercise
Why does git rebase main on feature-b produce conflicts after feature-a was squash-merged, while git rebase --onto origin/main feature-a feature-b does not?
Show answer
The squash commit on main has a different patch from A1 and A2, so Git does not recognize them as already applied and replays them. They then conflict with the squash commit's changes. With feature-a as the upstream, the range feature-a..feature-b excludes A1 and A2, so only B1 and B2 are replayed.