Sub topic 3
The problem: rebasing the top leaves the rest behind
What this chapter assumes
This chapter assumes you have read the earlier chapters on branches as pointers and on what a stack looks like. You should be comfortable with git rebase on a single branch, and you need Git 2.38 or newer for the later chapters. Here we only look at the problem. The fix comes in the next chapter.
The starting point
Take the stack from the previous chapter. Three branches, each built on the one below:
feature-1has one commit on top ofmain.feature-2has one commit on top offeature-1.feature-3has one commit on top offeature-2.
Each branch is a separate pull request, and each pull request targets the branch below it. Meanwhile, someone merges new work into main. Your stack is now based on an old main, and you want to bring it up to date.
The obvious move is to check out the top branch and rebase it:
git switch feature-3
git rebase mainFor a single branch this is exactly right. For a stack it only does part of the job.
Reproducing the problem
You can see this yourself in a throwaway repository. The script below builds the stack, adds a commit to main, and then rebases only the top branch. It assumes user.name and user.email are already configured.
git init -q -b main stack-demo
cd stack-demo
echo base > base.txt && git add base.txt && git commit -qm "Base"
git switch -q -c feature-1
echo one > one.txt && git add one.txt && git commit -qm "Add one"
git switch -q -c feature-2
echo two > two.txt && git add two.txt && git commit -qm "Add two"
git switch -q -c feature-3
echo three > three.txt && git add three.txt && git commit -qm "Add three"
git switch -q main
echo news > news.txt && git add news.txt && git commit -qm "Main moves on"
git switch -q feature-3
git rebase main
git log --oneline --graph --all --decorateThe commit IDs differ on your machine, but the shape of the output looks like this:
* c4d5e6f (HEAD -> feature-3) Add three
* b3c4d5e Add two
* a2b3c4d Add one
* 9f8e7d6 (main) Main moves on
| * 5e4d3c2 (feature-2) Add two
| * 4d3c2b1 (feature-1) Add one
|/
* 0a1b2c3 BaseRead the graph from the top. feature-3 now sits on main, and it carries copies of "Add one" and "Add two". But feature-1 and feature-2 did not move. They still point at the original commits, which hang off the old Base commit on a side line.
Why it happens
A rebase never edits commits. Commits are immutable, and their IDs are hashes of their content, including the parent ID. A rebase creates new commits with new parents and then moves one branch name to the last new commit.
Which branch name does it move? Only the one you had checked out. git rebase main on feature-3 does three things:
- It finds the commits reachable from
feature-3but not frommain. That is three commits: "Add one", "Add two" and "Add three". - It replays them, in order, on top of
main. This creates three new commits. - It moves the
feature-3ref to the last new commit.
Nothing in that procedure looks at feature-1 or feature-2. Git has no concept of a stack. To Git, those two branches are ordinary refs that happen to point at commits inside the range being replayed. The range gets copied, and the refs stay where they were.
The old commits are not deleted. They are still reachable through feature-1 and feature-2, which is exactly why they remain visible in the graph.
What the damage looks like
The stale branches cause different problems depending on where you notice them.
The branches look fine locally
Nothing warns you. git status is clean and git rebase reported success. The only sign is in the graph, or in how the pull requests behave later.
Pull requests disagree with each other
On the remote, feature-1 and feature-2 still contain the old commits. If you force-push only feature-3, its pull request now contains new copies of the commits that the other two pull requests also contain, under different IDs. Depending on the platform, the pull request for feature-3 may show three commits instead of one, or its diff may no longer be computed against the branch you expect.
The next change goes on the wrong base
If you now commit on feature-2, for example to address review feedback, that commit is built on the old "Add two", not on the rebased one. You now have two diverging histories of the same work. Reconciling them later means resolving the same conflicts twice.
Merging brings the old history back
Suppose feature-1 is approved and merged with a merge commit. The old "Add one" enters main. feature-3 carries a different copy of the same change. Git usually recognizes the identical patches and skips them when you rebase again, but that relies on the patches still being identical. If you resolved a conflict while rebasing, the copies differ, and you get duplicate changes or new conflicts.
Checking whether a stack is stale
You can test the relationship directly. A healthy stack means each branch is an ancestor of the one above it. git merge-base --is-ancestor answers that question through its exit status:
git merge-base --is-ancestor feature-1 feature-2 && echo "feature-1 ok" || echo "feature-1 stale"
git merge-base --is-ancestor feature-2 feature-3 && echo "feature-2 ok" || echo "feature-2 stale"In the demo repository after the plain rebase, both lines print stale, because the new feature-3 does not contain the old commits that the lower branches point to.
A quicker visual check is to list what feature-3 has beyond the branch below it:
git log --oneline feature-2..feature-3In a healthy stack this prints one commit. After a plain rebase it prints all three, because none of them are reachable from the old feature-2.
The manual fix, and why people avoid it
You can repair the stack by hand. Rebase from the bottom up, moving each branch onto the one below:
git rebase main feature-1
git rebase feature-1 feature-2
git rebase feature-2 feature-3This works when everything replays cleanly. It has real costs:
- You run one rebase per branch, and a stack of six means six commands in the right order.
- Conflicts are resolved once per branch. A conflict in "Add one" can come back in the later rebases, because each of them replays the commits again.
- If you already rebased
feature-3as in the demo, the later steps need care. Movingfeature-2onto the newfeature-1relies on Git recognizing the old "Add one" as a duplicate. Git does this by comparing patch contents, and it fails if the commit changed while you resolved a conflict. The precise repair then needsgit rebase --ontowith the old tip of the branch, which you must dig out of the reflog.
None of this is hard once you understand it, but it is repetitive and easy to get wrong. Keeping a whole stack current becomes a chore that discourages stacking.
What a fix has to do
The root cause is that one rebase moves one ref. A fix has to teach the rebase about the other refs. Specifically, while replaying the commits, Git would need to notice that an old commit is the tip of another branch, and after creating its replacement, move that branch to it.
Git can do exactly that from version 2.38. The option is --update-refs, and the next chapter covers it.
Summary of the failure
| Step | What happens to `feature-3` | What happens to `feature-1` and `feature-2` |
|---|---|---|
| `git rebase main` on `feature-3` | Three new commits are created and the ref moves to them | Nothing. Their refs still point at the old commits |
| Graph afterward | Sits on the new `main` | Sit on the old base, on a side line |
| Pull requests | Contains copies of lower commits | Still contain the old versions |
Exercise
You run git switch feature-3 and git rebase main on a three-branch stack. Afterward, git log --oneline feature-2..feature-3 prints three commits instead of one. Explain why, and name which refs the rebase moved.
Show answer
The rebase replayed all three stack commits as new commits and moved only the feature-3 ref. feature-2 still points at the old "Add two" commit, which is not an ancestor of the new feature-3. So none of the three new commits are reachable from feature-2, and the range feature-2..feature-3 lists all of them. Only feature-3 was moved. feature-1 and feature-2 are stale.
Exercise
Why does Git not move feature-1 and feature-2 during a plain git rebase main, even though their commits are being replayed?
Show answer
A rebase only updates the branch you have checked out. Git treats the other branches as unrelated refs. The replayed commits are copies, and the old commits keep existing under their old IDs, so any ref pointing at them stays valid and is left alone.