Sub topic 12
Optional: rebasing a stack of branches with --update-refs
This chapter is optional. Skip it if you never keep one branch on top of another. If you do, --update-refs saves you from re-pointing each branch by hand after a rebase.
It assumes you have read the earlier chapters on git rebase --onto and the upstream..branch range. You should be comfortable with git log --graph, and you should know how to force-push safely. You need Git 2.38 or later. Check with git --version.
The problem: a rebase moves only one branch
A stacked branch workflow keeps small, dependent branches on top of each other. Each branch holds one reviewable step:
feature-astarts frommain.feature-bstarts fromfeature-a.feature-cstarts fromfeature-b.
When main moves forward, you rebase the top of the stack. Suppose you run this from feature-c:
git rebase mainGit replays every commit in main..feature-c. That range includes the commits of feature-a and feature-b, because they sit underneath feature-c. Git copies all of those commits, but only the branch you are rebasing, feature-c, moves to the new copies. The branches feature-a and feature-b still point at the old commits, so the stack is broken. You would have to fix each pointer yourself, for example with git branch -f feature-b <new-commit>.
What --update-refs does
With --update-refs, Git looks at every branch that points at a commit inside the range it is about to replay. When the rebase finishes, it moves each of those branches to the rewritten copy of the commit it used to point at.
The effect is that one command rebases the whole stack. You run it from the top branch, and the lower branches follow.
The option has these properties:
- It was added in Git 2.38.
- It works with the merge backend, which is the default today. It does not work with the old
--applybackend. - It works with and without
--interactive. - It only moves branches that point into the replayed range. It never touches the upstream commit or anything below it.
- It skips a branch that is checked out in another worktree, because Git will not move a branch someone is using. Git prints a warning, and you move that branch yourself.
A runnable demonstration
The following script builds a three-branch stack, advances main, and rebases the stack. It uses a throwaway repository named stack-demo in the current directory.
set -e
rm -rf stack-demo
git init -q -b main stack-demo
cd stack-demo
git config user.name demo
git config user.email [email protected]
echo base > base.txt && git add base.txt && git commit -q -m "base"
git switch -q -c feature-a
echo a > a.txt && git add a.txt && git commit -q -m "A1"
git switch -q -c feature-b
echo b > b.txt && git add b.txt && git commit -q -m "B1"
git switch -q -c feature-c
echo c > c.txt && git add c.txt && git commit -q -m "C1"
# main moves forward while the stack waits for review.
git switch -q main
echo m > m.txt && git add m.txt && git commit -q -m "M1"
# Rebase the whole stack from its top branch.
git switch -q feature-c
git rebase --update-refs main
git log --oneline --graph --decorate main feature-a feature-b feature-cThe final git log shows one straight line. The commits M1, A1, B1 and C1 appear in that order, and the labels are attached like this:
mainis onM1.feature-ais on the newA1.feature-bis on the newB1.feature-cis on the newC1.
Run the script again with the rebase line changed to plain git rebase main. feature-a and feature-b then stay on the old commits, and the graph shows two separate lines.
What the interactive todo list shows
--update-refs works by adding extra lines to the rebase todo list. To see them, run an interactive rebase from feature-c:
git rebase -i --update-refs mainThe editor opens with something like this:
pick 1111111 A1
update-ref refs/heads/feature-a
pick 2222222 B1
update-ref refs/heads/feature-b
pick 3333333 C1Each update-ref line means "when you reach this point, remember that this branch should end up here". Git records the new commit and applies all the updates when the whole rebase finishes. The branch you are rebasing, feature-c, has no line because the rebase moves it anyway.
You can edit this list:
- Delete an
update-refline to leave that branch where it is. - Reorder
picklines, and theupdate-reflines follow the commit they were attached to only if you move them with it. - Drop a
pickline, and the branch that pointed at that commit lands on the closest earlier commit that was kept.
Because the updates happen at the end, git rebase --abort leaves every branch exactly where it was. A conflict halfway through does not leave you with some branches moved and others not.
Combining it with --onto
The spine of this tutorial is --onto, and --update-refs works with it too. The common case is the one from the previous chapters: the parent branch was squash-merged, and the branches above it must be restacked.
Say feature-a was squash-merged into main on the remote. The branches feature-b and feature-c still contain the original A1 commit, which Git cannot recognize as already merged. You want to drop feature-a from the stack and keep feature-b and feature-c in order:
git fetch origin
git switch feature-c
# Preview: these are the commits that will be replayed.
git log --oneline feature-a..feature-c
git rebase --update-refs --onto origin/main feature-a feature-cThe upstream is feature-a, so the replayed range is feature-a..feature-c, which holds B1 and C1. Git places them on origin/main. The branch feature-b points into that range, so it moves to the new B1. The branch feature-a is the upstream and lies outside the range, so it is not touched. You can delete it after you confirm the result.
Making it the default
If you work with stacks regularly, turn it on once:
git config --global rebase.updateRefs trueAfter that, every git rebase behaves as if you had passed --update-refs. This is safe for most workflows, because a branch only moves when it points into the range being replayed. To switch it off for one command, pass --no-update-refs.
When it does something you did not expect
The option is simple, but these cases surprise people:
| Situation | What happens |
|---|---|
| A branch is checked out in another worktree | Git skips it and prints a warning. Move it yourself with `git branch -f`. |
| A branch points at a commit that you drop | The branch moves to the nearest earlier commit that was kept. |
| A branch points at the upstream commit or below it | It is outside the range and does not move. |
| A branch points into the range but you do not want it moved | Delete its `update-ref` line in an interactive rebase, or use `--no-update-refs`. |
| A branch moves while the rebase is paused (for example, you commit to it from another terminal) | Git refuses to overwrite it at the end and tells you which ref it left alone. |
| You abort the rebase | No branch moves. |
Unrelated branches that happen to share a commit with your stack also move. For example, a scratch branch that points at B1 will be updated to the new B1. Run git branch --contains <commit> or git log --decorate before you rebase if you are not sure what points where.
Recovering from a bad result
--update-refs rewrites several branches in one command, so one git reset --hard ORIG_HEAD is not enough to undo it. ORIG_HEAD only covers the branch you were on. Each moved branch has its own reflog, though:
git reflog show feature-b
git branch -f feature-b feature-b@{1}Check the reflog for each branch that moved. The entry before the rebase is the old tip. Before a risky rebase, a cheap safeguard is to tag or branch the old tips:
git branch backup/feature-b feature-b
git branch backup/feature-c feature-cChecklist for restacking a chain
- Run
git --versionand confirm it is 2.38 or later. - Switch to the top branch of the stack.
- Preview the range with
git log --oneline <upstream>..<top-branch>. - Run
git rebase --update-refs [--onto <newbase>] <upstream>from the top branch. With a plain upstream, the new base is the upstream itself. - Check the shape with
git log --oneline --graph --decoratelisting every branch in the stack. - Push all rewritten branches with
--force-with-lease.
Exercise
A stack is main <- feature-a <- feature-b <- feature-c. You are on feature-c and run git rebase main after main gained a new commit. Afterwards git log --oneline --graph --decorate main feature-a feature-b feature-c shows feature-a and feature-b on a separate line from feature-c. What went wrong, and what command would have moved all three branches?
Show answer
A plain rebase moves only the checked-out branch, feature-c. Git copied the commits of feature-a and feature-b as part of main..feature-c, but feature-a and feature-b still point at the originals. Running git rebase --update-refs main from feature-c would have moved feature-a and feature-b to the copied commits as well.
Exercise
feature-a was squash-merged into origin/main. You are on feature-c, which sits on feature-b, which sits on feature-a. Write the command that restacks feature-b and feature-c onto origin/main and moves both branches. Then say which commits are replayed and why feature-a does not move.
Show answer
git rebase --update-refs --onto origin/main feature-a feature-c. The replayed range is feature-a..feature-c, which contains the commits of feature-b and feature-c. Both branches point into that range, so both move. feature-a is the upstream, so it is outside the range and Git leaves it alone.
Exercise
You start a rebase with --update-refs, hit a conflict you cannot resolve, and run git rebase --abort. Where are feature-a and feature-b afterwards, and why?
Show answer
They are exactly where they were before the rebase. Git applies all the update-ref changes only when the rebase finishes, so an aborted rebase moves no branch.