Sub topic 12
Optional: rebasing a stack of branches with --update-refs
This chapter assumes you can run git rebase --onto <newbase> <upstream> <branch> and know that a branch is a pointer to a commit. It also assumes Git 2.38 or later, which added --update-refs.
The problem with stacks
A stack is a chain of branches where each one starts from the one before it. Here feature-b was started from feature-a, which was started from main.
When main moves on, you rebase the top of the stack. The command git rebase --onto main old-base feature-b replays every commit in old-base..feature-b, including the commits that belong to feature-a. After it runs, feature-b sits on the new main. The feature-a branch still points at the old, pre-rebase commits.
Without --update-refs you would then have to rebase feature-a by hand, and then rebase feature-b again on top of it. That is easy to get wrong.
What --update-refs does
With --update-refs, Git looks at every other branch that points into the range being replayed. When the rebase finishes, it moves each of those branches to the rewritten copy of the commit it used to point at.
The command has the same shape as before, with one extra flag:
git rebase --update-refs --onto main old-base feature-bThe three arguments mean what they did earlier. The new base is main. The upstream, old-base, bounds the range. The branch to move is feature-b. The flag only adds the step of carrying feature-a along.
A runnable example
This script builds a small stack in a throwaway repository and rebases it. It needs Git 2.38 or later.
set -e
git init -q -b main stack-demo
cd stack-demo
git config user.name demo
git config user.email [email protected]
c() { echo "$1" > "$1.txt"; git add "$1.txt"; git commit -q -m "$1"; }
c base
git tag old-base
git switch -q -c feature-a
c a1
c a2
git switch -q -c feature-b
c b1
git switch -q main
c main2
git rebase --update-refs --onto main old-base feature-b
git log --oneline --graph --decorate --allIn the final log, feature-a and feature-b both sit on top of main2, and feature-a points at the copy of a2. If you leave out --update-refs and run the script again, feature-a stays on the old a2, and git log --all shows a second line of history.
Making it the default
To turn the flag on for every rebase, set the config option:
git config --global rebase.updateRefs trueYou can then pass --no-update-refs for the occasional rebase where you want other branches left alone.
Gotchas
- Branches in other worktrees are skipped. Git does not move a branch that is checked out in another worktree. It warns instead, so read the output.
- Updates happen at the end. Git records the branch updates in the rebase state and applies them only when the rebase finishes. If you run
git rebase --abort, no branch moves. - Conflicts still stop the rebase. You resolve them as usual with
git rebase --continue. The extra branches move after the last commit is replayed. - Every moved branch needs its own push. Each updated branch has rewritten history. Push each one with
git push --force-with-lease origin <branch>. - Only branches inside the range move. A branch that points outside
upstream..branchis left alone.
Exercise
Exercise
You have the stack main -> feature-a -> feature-b -> feature-c. The tag old-base marks where main was when you started. You want all three branches moved onto the current main with one command. Which command do you run, and why does it need the --update-refs flag?
Show answer
Run git rebase --update-refs --onto main old-base feature-c. This replays everything in old-base..feature-c onto main. Without the flag, only feature-c is moved, and feature-a and feature-b still point at the old commits. With the flag, Git moves both of them to the rewritten commits when the rebase finishes.
Exercise
After the rebase from the previous exercise, feature-a was not moved, and Git printed a warning. What is the most likely cause, and how do you fix it?
Show answer
The branch feature-a is most likely checked out in another worktree, and Git does not move branches that are checked out elsewhere. Either switch that worktree to a different branch and repeat the rebase, or rebase feature-a by hand with git rebase --onto main old-base feature-a and then rebase the later branches on top of it.