Sub topic 4
Rebasing the whole stack with --update-refs
This chapter assumes you are comfortable with git add, commit, switch, and log. It also assumes you know a branch is a movable pointer to a commit and that you have used git rebase on a single branch. You need Git 2.38 or newer.
The problem in one picture
Suppose you have a stack: main, then feature-1, feature-2, and feature-3, each built on the one before. Meanwhile main has moved forward on the remote. You want the whole stack to sit on top of the new main.
If you run git rebase origin/main while on feature-3, Git rewrites every commit in the stack. But it only moves the branch you have checked out. feature-1 and feature-2 still point at the old, pre-rebase commits. They are now stale, and you would have to move each one by hand.
Using --update-refs
The --update-refs option tells Git to also move every local branch that points at a commit being rebased. Run it from the top of the stack:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainGit finds feature-1 and feature-2 because they point at commits inside the range it is replaying. When the rebase finishes, all three branches sit on the new main, in the same order as before.
Check the result with the same log command you used to build the stack:
git log --oneline --decorate --graph origin/main..feature-3You should see feature-1, feature-2, and feature-3 each decorating a commit, in a straight line above origin/main.
How it works
Git does not run a separate step for each branch. It adds extra instructions to the rebase's todo list. After the last commit that belongs to a branch, it inserts an update-ref line. When the rebase reaches that line, it moves the branch to the commit it just rewrote.
A todo list for a stack looks like this:
pick 1a2b3c4 Add parser skeleton
update-ref refs/heads/feature-1
pick 5d6e7f8 Add parser tests
pick 9a8b7c6 Wire parser into CLI
update-ref refs/heads/feature-2
pick 0f1e2d3 Document CLI flagHere feature-1 has one commit, feature-2 has two more, and feature-3 has one more. There is no update-ref line for feature-3. It is the checked-out branch, so Git updates it at the end of any rebase.
If you run git rebase -i --update-refs origin/main, you see these lines in your editor. Moving a pick line across an update-ref line changes which branch the commit belongs to.
Making it the default
Typing the flag every time is easy to forget. Turn it on globally:
git config --global rebase.updateRefs true
# Turn it off for a single rebase:
git rebase --no-update-refs mainCommon errors
- Rebasing from the middle. If you run the rebase while on
feature-2,feature-3is not touched, because it is not in the range being replayed. Always rebase from the top of the stack. - Forgetting to fetch.
origin/mainis only as fresh as your lastgit fetch. Fetch first. - Expecting the remote to change.
--update-refsmoves local branches only. The remote copies of all three branches still have the old commits. Pushing them is covered in a later chapter. - Branches checked out elsewhere. A branch checked out in another worktree is not moved. Git warns you instead. Later chapters cover this limit.
Exercise
You are on feature-3 in a stack of main, feature-1, feature-2, and feature-3. You run git rebase origin/main without any extra option. Which branches now sit on the new main, and what single option would have moved the others?
Show answer
Only feature-3 sits on the new main. feature-1 and feature-2 still point at the old commits. Running git rebase --update-refs origin/main from feature-3 moves all three.
Exercise
A todo list shows update-ref refs/heads/feature-1 after the first pick line. What does Git do when the rebase reaches that line?
Show answer
It moves the branch feature-1 to the rewritten commit produced by the pick line just before it.