Sub topic 4
Rebasing the whole stack with --update-refs
This chapter assumes you have used git rebase on a single branch and know that a branch is a pointer to a commit. You need Git 2.38 or newer, because that release added --update-refs. Check with git --version.
The situation
Suppose you have a stack of three branches. feature-1 branches off main, feature-2 branches off feature-1, and feature-3 branches off feature-2. Meanwhile main gets a new commit, so every branch in the stack is now based on an old main. You want all three to sit on the new main again.
Rebasing is the right tool, but rebasing one branch only moves the branch you have checked out. The other two branch names keep pointing at the old commits.
Reproduce the problem
The following script builds a small stack in a throwaway repository. It uses a local main as the base instead of origin/main, so you can run it without a remote.
git init -b main stack-demo
cd stack-demo
echo base > base.txt
git add base.txt
git commit -m "Base"
git switch -c feature-1
echo one > f1.txt
git add f1.txt
git commit -m "Add feature 1"
git switch -c feature-2
echo two > f2.txt
git add f2.txt
git commit -m "Add feature 2"
git switch -c feature-3
echo three > f3.txt
git add f3.txt
git commit -m "Add feature 3"
# main moves ahead
git switch main
echo news > news.txt
git add news.txt
git commit -m "Update main"Now rebase only the top branch with a plain rebase and look at all the branches:
git switch feature-3
git rebase main
git log --oneline --decorate --graph --allThe output looks like this. Your commit IDs will differ.
* 9c1d0e2 (HEAD -> feature-3) Add feature 3
* 4b7a5f1 Add feature 2
* 2e8c3a9 Add feature 1
* 7f6d4c0 (main) Update main
| * 5a9b2d8 (feature-2) Add feature 2
| * 1c4e6b7 (feature-1) Add feature 1
|/
* 3d2f1a0 BaseGit copied the three commits onto the new main. The rewritten copies are in the top chain, and only feature-3 points at them. feature-1 and feature-2 still point at the original commits, which are now off to the side. Those branches are stale, and a pull request opened from them would show the old history.
Before Git 2.38, the fix was to move each branch by hand, for example git branch -f feature-2 4b7a5f1, working from the bottom up. That is slow and easy to get wrong.
Rebase everything at once
Reset the demo by recreating it, or run git rebase --abort if you are mid-rebase. Then rebase the top branch with the option:
git switch feature-3
git rebase --update-refs mainGit rebases feature-3 as before. It also notices that feature-1 and feature-2 point at commits inside the range being rewritten, and it moves them to the rewritten copies. When the rebase finishes, Git reports which branches it moved:
Successfully rebased and updated refs/heads/feature-3.
Updated the following refs with --update-refs:
refs/heads/feature-1
refs/heads/feature-2Check the result:
git log --oneline --decorate --graph* 9c1d0e2 (HEAD -> feature-3) Add feature 3
* 4b7a5f1 (feature-2) Add feature 2
* 2e8c3a9 (feature-1) Add feature 1
* 7f6d4c0 (main) Update main
* 3d2f1a0 BaseThe stack is a straight line again, and each branch name sits on the last commit of its own layer.
What the option does internally
Branches are refs, and a ref is a named pointer. A rebase never edits commits. It creates new commits that copy the old ones, then repoints a single ref at the new tip. --update-refs extends that last step to more refs.
When the rebase starts, Git works out which commits it will replay. For each of those commits, it checks whether another local branch points at it. Where one does, Git records that the branch should move to whatever new commit replaces that one. Git applies the moves when the rebase completes. If you run git rebase --abort, the branches stay where they were.
Three details follow from this:
- Only local branches under
refs/heads/are considered. Remote-tracking branches such asorigin/feature-1are not changed. Your remote still has the old commits until you push. - Only branches pointing inside the replayed range are moved. A branch that points at
mainor at any commit outside the range is left alone. - The option works with the normal, non-interactive
git rebase. You do not need-i.
Choosing the right target
The command rebases the branch you have checked out, so check out the top of the stack first. Running it from a lower branch only moves that branch and the ones beneath it. The layers above stay on the old commits.
The upstream argument is the new base. For a stack on a shared remote, fetch first and rebase onto the remote-tracking branch:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainThis avoids touching your local main, which you may not have updated. The same command works if you leave out the argument and the top branch has an upstream configured, but naming the base explicitly makes the result easier to predict.
Checking your work
A quick sanity check after any restack is to confirm that every layer is based on the one below it:
git merge-base --is-ancestor main feature-1 && echo "feature-1 is on main"
git merge-base --is-ancestor feature-1 feature-2 && echo "feature-2 is on feature-1"
git merge-base --is-ancestor feature-2 feature-3 && echo "feature-3 is on feature-2"Each command prints its message only if the first branch is an ancestor of the second. A missing line tells you which layer is stale.
You can also count the commits each layer adds:
git rev-list --count main..feature-1
git rev-list --count feature-1..feature-2
git rev-list --count feature-2..feature-3For a healthy stack, each number matches the number of commits you intended for that layer. A layer that shows far too many commits usually means a branch stayed on old history.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Lower branches still show the old commits | Rebased without `--update-refs` | Move each branch by hand, or recover with the reflog, then repeat with the option |
| Only some branches moved | You rebased from a middle branch, so the upper branches were outside the range | Check out the top branch and rebase from there |
| A branch did not move although it points inside the range | It is checked out in another worktree (covered in a later chapter) | Move it by hand or free that worktree |
| `unknown option` error | Git is older than 2.38 | Upgrade Git |
| Remote branches look unchanged | `--update-refs` only touches local refs | Push the stack (covered in a later chapter) |
When the rebase stops
If a commit conflicts, the rebase pauses as it does for any rebase. Resolve the files, run git add, and continue with git rebase --continue. Branches that Git has not yet reached keep their old positions until the rebase finishes, which is why aborting is safe. A later chapter treats conflicts in the middle of a stack in more detail.
Summary of the model
- A stack is a chain of branches, and each branch is a ref to one commit.
- A rebase creates new commits and moves one ref.
--update-refsmoves every local branch that pointed inside the replayed range, so the whole stack stays connected.- Always run it from the top branch.
Exercise
You have the stack main, feature-1, feature-2, feature-3, and feature-3 is checked out. After git fetch origin, you run git rebase origin/main without the update option. Then you open git log --oneline --decorate --graph --all. Where do feature-1 and feature-2 point, and what single command would have avoided the problem?
Show answer
Both still point at the original commits, which sit on the old base, so they appear as a separate side line of history. Only feature-3 moved to the rewritten commits. The command git rebase --update-refs origin/main, run from feature-3, would have moved all three branches.
Exercise
You are on feature-2 in a stack where feature-3 is built on top of it. You run git rebase --update-refs origin/main. Which branches end up on the new base, and which do not?
Show answer
feature-2 and feature-1 are rebased or moved onto the new base, because both are inside the range being replayed. feature-3 does not move, because it sits above the checked-out branch and is outside that range. It remains on the old commits. To restack all three, check out feature-3 and rebase from there.