Sub topic 3
The problem: rebasing the top leaves the rest behind
This chapter assumes you know that a branch is a movable pointer to a commit, and that you have used git rebase on a single branch. You should also know how remote branches and pull requests relate. Nothing here needs Git 2.38 yet. That version matters in the next chapter, where the fix appears.
The starting point
Suppose you built a stack of three branches, each created from the one before it:
git switch -c feature-1 main
# ...commit work...
git switch -c feature-2
# ...commit work...
git switch -c feature-3
# ...commit work...Each branch has its own pull request. feature-1 targets main, feature-2 targets feature-1, and feature-3 targets feature-2. Meanwhile a teammate merges a change into main, and you want your stack to sit on top of it.
What a plain rebase does
The natural move is to rebase the branch you have checked out:
git fetch origin
git switch feature-3
git rebase origin/mainGit finds the commits reachable from feature-3 but not from origin/main. That range includes the commits of all three branches. It replays them one by one on top of origin/main. Replaying creates new commits with new IDs, because a commit ID depends on its parent.
When the replay finishes, Git moves one pointer: the branch you are on, feature-3. It has no reason to touch any other branch. feature-1 and feature-2 still point at the original commits.
The copies A', B', and C' hold the same changes as A, B, and C. Git sees them as unrelated commits, because their IDs differ.
Seeing the damage
Check whether feature-1 is still part of feature-3:
git merge-base --is-ancestor feature-1 feature-3
echo $?Before the rebase this prints 0, meaning yes. After it, the command prints 1. The stack is no longer a chain. You can also look at the whole picture:
git log --oneline --decorate --graph --allThe graph shows two parallel lines of history, with feature-3 on the new one and feature-1 and feature-2 on the old one.
Why this hurts
- Pull requests go stale. The PR for
feature-1still shows the old base, so it does not include your teammate's change frommain. CI runs against outdated code. - Duplicate work appears. If someone merges
feature-1as it stands,feature-3still containsA'. Git then has two commits with the same change and different IDs. You can get conflicts or repeated hunks. - The stack stops being a stack. Later edits to
feature-1do not reachfeature-3until you rebase again.
The manual repair is to move each branch yourself, working from the bottom up. For every branch you find the new copy of its tip commit and run git branch -f to point the branch at it. This works, but you must identify the right copies by hand. A wrong pick silently drops or duplicates commits.
The idea behind the fix
The root cause is that a rebase moves only the branch you are on, while a stack needs several pointers to move together. Git can rewrite those pointers during the same rebase, because it knows which commits in the replayed range have other branch names on them. The next chapter shows that option, --update-refs.
Exercise
You rebase feature-3 onto origin/main in a stack of main, feature-1, feature-2, and feature-3. Which branch pointers does Git move, and why is feature-1 now a problem?
Show answer
Git moves only feature-3, the checked-out branch. feature-1 and feature-2 still point at the old commits, so they are no longer ancestors of feature-3. Their pull requests have an outdated base, and the changes they contain also exist in feature-3 as copies with different IDs.