Sub topic 7
What --update-refs does not touch
This chapter assumes you have used git rebase --update-refs on a stack and know that a branch is a pointer to a commit. The option is useful, but it moves fewer refs than people expect. Knowing what it skips explains most "why is this branch still on the old base?" surprises.
What the option moves
--update-refs moves a local branch only if it points at a commit that the rebase is replaying. Git adds an update-ref step after that commit and moves the branch to the rewritten commit. Git records the new positions during the rebase and applies them when the rebase finishes.
Everything else stays where it was.
| Ref | Moved by `--update-refs`? | Why |
|---|---|---|
| Local branch inside the rebased range | Yes | Its commit is being rewritten |
| Local branch checked out in another worktree | No | Git will not move a branch another working directory is using |
| Branch above the tip you rebased | No | Its commits are not part of the range |
| Remote-tracking branch such as `origin/feature-1` | No | Only a fetch or push changes it |
| Tag | No | Only branches are updated |
| Branch that is not an ancestor of the tip | No | Its commits are not in the range |
Branches checked out in other worktrees
A linked worktree is a second working directory attached to the same repository. Each branch can be checked out in only one worktree at a time. Git leaves a branch alone if any worktree has it checked out, because moving it would change files under that worktree.
git worktree add ../review feature-1
git switch feature-3
git rebase --update-refs origin/mainAfter this rebase, feature-2 and feature-3 sit on the new base. feature-1 is skipped because ../review has it checked out, so it still points at the old commits. The rest of the stack now sits on top of a base that feature-1 no longer matches.
The simplest prevention is to free the branch before you rebase:
git worktree list
git -C ../review switch --detachIf you already rebased, find the rewritten commit with git log --oneline --decorate. Then point the branch at it with git branch -f feature-1 <commit>, after you have freed the branch in the other worktree.
Branches above the tip
The rebase starts from the branch you have checked out, and only the commits below it are replayed. If you rebase feature-2 while feature-3 exists on top of it, feature-3 is not part of the range. It keeps pointing at the old commits of feature-2.
To restack the whole chain, rebase from the top branch. If you must work on a middle branch, rebase it and then bring each higher branch over with git rebase --onto, covered in a later chapter.
Remote refs and tags
origin/feature-1 is a remote-tracking branch, a local record of where the remote branch was at your last fetch. --update-refs leaves it alone. After the rebase, your local branch and its remote counterpart have diverged, so a plain git push is rejected. You push the rewritten branches with --force-with-lease.
Tags are not moved either. A tag on a commit in the range keeps pointing at the old commit. If you tag stack commits for review, move those tags yourself with git tag -f.
Aborting leaves branches alone
Because the ref updates are applied at the end, git rebase --abort leaves every branch where it was. That makes a conflicted restack safe to back out of. After a completed rebase, the reflog of each moved branch holds its previous position.
Quick check after a restack
Run one command to confirm every branch in the stack now sits on the new base:
git log --oneline --decorate --graph origin/main..feature-3Each branch name should appear on a commit in one straight line above origin/main. A branch name that sits on a separate, older line was skipped.
Exercise
You run git rebase --update-refs origin/main from feature-3. Afterward, feature-1 still points at its old commits. git worktree list shows feature-1 checked out in ../review. Why did this happen, and how do you fix it?
Show answer
Git does not move a branch that is checked out in another worktree, so it skipped feature-1. Free the branch in the other worktree with git -C ../review switch --detach. Then find the rewritten commit with git log --oneline --decorate and run git branch -f feature-1 <commit>. Next time, free the branch before you rebase.
Exercise
After a successful --update-refs rebase, git push for feature-2 is rejected. Which ref did the rebase not update, and which push option is the safe way to publish the result?
Show answer
The remote-tracking branch origin/feature-2 was not updated, and the remote branch still has the old commits. Push with git push --force-with-lease origin feature-2, which refuses to overwrite the remote branch if it moved since your last fetch.