Sub topic 7
What --update-refs does not touch
This chapter assumes you can run git rebase on a single branch and read git log --graph output. It also assumes you have a stack of two or more branches and Git 2.38 or newer. The earlier chapters showed that git rebase --update-refs moves every branch in a stack in one rebase. Here you will learn where that guarantee ends, how to notice the gaps, and how to close each one.
The rule behind every limit
--update-refs looks at the commits the rebase is about to replay. It adds an update-ref step for each local branch whose tip is one of those commits. Everything else is outside its view. When it seems to miss something, ask three questions:
- Is the thing a local branch (a name under
refs/heads/)? - Does it point at a commit that is in the range being rebased?
- Is Git allowed to move it right now?
If any answer is no, the ref stays where it was. The diagram applies the same test.
Branches checked out in other worktrees
A worktree is an extra working directory attached to the same repository, created with git worktree add. Git refuses to move a branch out from under a worktree that has it checked out, because that worktree's files and index would no longer match its branch. So --update-refs skips such branches. The Git documentation states that branches checked out in a worktree are not updated this way.
The skipped branch keeps pointing at the old commits. If it is feature-2 in the middle of your stack, the stack is now broken at that point. feature-1 and feature-3 sit on the new base, and feature-2 sits on the old one.
List your worktrees before a restack:
git worktree listThe output looks like this:
/home/dev/project 3f2a1b9 [feature-3]
/home/dev/project-review 8c7d6e5 [feature-2]Here feature-2 is checked out in project-review, so the rebase you run in project will not move it. The simplest fix is to detach that worktree's HEAD for the duration of the rebase. A detached HEAD points at a commit instead of a branch, so the branch is free to move.
git -C ../project-review switch --detach
git rebase --update-refs origin/main
git -C ../project-review switch feature-2Detaching needs a clean working tree in that worktree. If it has uncommitted changes, commit or stash them first. If you no longer need the worktree, git worktree remove ../project-review does the same job permanently.
The other direction also works. If you run the rebase from the worktree that has the top branch checked out, any other worktree holding a stack branch still blocks that branch.
Remote branches and upstream settings
--update-refs only rewrites branches under refs/heads/. Remote-tracking branches such as origin/feature-1 are your local record of what the server has. They change only when you fetch or push. After the rebase, origin/feature-1 still points at the old commits, and git status on feature-1 reports that the branches have diverged.
That divergence is expected. Publishing the rewritten branches is a separate step, covered in the chapter on pushing a stack with --force-with-lease. Nothing about the rebase updates the server, and nothing changes your branch's configured upstream either.
Tags
A tag is a ref under refs/tags/, and --update-refs ignores it. A tag that pointed at a commit in your stack still points at the old, now-unreachable version after the rebase. Tags on stack commits are unusual, but they appear when someone marks a review checkpoint. Move one with git tag -f NAME NEW-COMMIT, or delete it if it was only a bookmark.
Branches that point outside the rebased range
The range is the commits between the new base and the branch you rebase. A branch that does not point into that range is not moved. This produces three common cases.
- A branch at or below the old base. A local
mainthat points at the old base is not in the range, so it is not moved. That is correct. If you want it to advance, update it yourself withgit fetch origin main:main. - A branch above your tip. If
feature-4was built onfeature-3and you rebasefeature-3, the rebase does not seefeature-4. Its tip is not in the range, because the range ends atfeature-3. Rebase from the top of the stack, not from the middle. - A sibling branch. Suppose you branched
feature-2bofffeature-1to try an alternative, and you are rebasingfeature-3. The tip offeature-2bis on a different line of commits from the range being rebased, so it is left alone.
The sibling case is the one that bites. Here is the layout before the rebase:
main - A - B (feature-1) - C (feature-2) - D (feature-3)
\
E (feature-2b)After git rebase --update-refs origin/main from feature-3, feature-1, feature-2, and feature-3 have new commit IDs. feature-2b still has E on top of the old B. Its history now contains a second, stale copy of B.
Repair it with --onto. Take the old tip of feature-1 as the upstream, so only feature-2b's own commits are replayed:
git rebase --onto feature-1 feature-1@{1} feature-2bfeature-1@{1} means "where feature-1 pointed before its latest change". It works here because the rebase moved feature-1 exactly once. If you have moved the branch since, read the reflog and pick the right entry. A safer habit is to mark the old tip before you start:
git branch old-feature-1 feature-1
git rebase --update-refs origin/main
git rebase --onto feature-1 old-feature-1 feature-2b
git branch -d old-feature-1The -d flag may refuse to delete old-feature-1 and say it is not fully merged. In that case the old commits are no longer reachable from anything else. Use -D only after you have confirmed the rebase result is right.
What updates happen, and when
During the rebase, each update-ref step records where its branch should end up. Git applies the recorded updates when the whole rebase finishes. Two consequences follow:
- If you run
git rebase --abort, no branch has moved. Aborting is safe in the middle of a stack. - If the rebase stops on a conflict, the lower branches are not yet updated. Resolve the conflict and run
git rebase --continue, and the updates land at the end.
You can inspect the plan while a rebase is paused:
git statusThe output includes the commands done and remaining. This is the same list you saw in the previous chapter, with its update-ref lines.
Detecting a missed branch
Do not trust that every branch moved. Check. Two commands cover it.
The first shows every branch in the stack on one graph. A name that appears on a different line of commits has been left behind.
git log --oneline --decorate --graph origin/main..feature-3The second asks which local branches contain a given commit. If an old commit is still reachable from a branch, that branch is stale.
git for-each-ref --format='%(refname:short) %(objectname:short)' refs/heads
git branch --contains OLD-COMMITReplace OLD-COMMIT with a commit ID from before the rebase, which you can read from git reflog. Each branch the second command prints still holds the old history. After a correct restack it should print nothing.
You can also compare old and new versions of a branch with git range-diff. It matches commits by content, which shows whether the rewrite changed anything beyond the base:
git range-diff origin/main feature-1@{1} feature-1Summary table
| Item | Moved by `--update-refs`? | What to do |
|---|---|---|
| Local branch whose tip is in the rebased range | Yes | Nothing |
| Branch checked out in another worktree | No | Detach or remove that worktree, or rebase it separately |
| Remote-tracking branch such as `origin/feature-1` | No | Push with `--force-with-lease` |
| Tag on a stack commit | No | Move it with `git tag -f` or delete it |
| Sibling branch off a stack commit | No | `git rebase --onto` |
| Branch above the tip you rebased | No | Rebase from the top of the stack |
| Local `main` | No | `git fetch origin main:main` |
Common mistakes
- Rebasing a stack with a review worktree open, then pushing. You publish a stack where one branch still contains old commits, and the pull request shows duplicate changes.
- Rebasing from the middle of a stack. Branches above the tip you chose are left on the old commits.
- Assuming a successful rebase means every name moved. The command exits with success even when it skipped branches.
Exercise
Your stack is main, feature-1, feature-2, feature-3. A second worktree has feature-2 checked out. You run git rebase --update-refs origin/main from the feature-3 worktree. Which branches move, what state is the stack left in, and how would you have avoided it?
Show answer
feature-3 is rebased because it is the branch being rebased, and feature-1 moves because it is a local branch pointing into the rebased range. feature-2 is not moved, because it is checked out in another worktree. It still points at commits built on the old base, so the stack is split. Before the rebase, run git switch --detach in the other worktree (or remove it), then run the rebase, then switch that worktree back to feature-2. To repair after the fact, rebase feature-2 with git rebase --onto feature-1 followed by the old feature-1 tip, then rebase feature-3 onto the new feature-2.
Exercise
You branched feature-2b off feature-1 as an experiment, then ran git rebase --update-refs origin/main from feature-3. git log --graph shows feature-2b on the old commits. Write the command that moves it onto the new feature-1, assuming feature-1 has been rewritten exactly once.
Show answer
git rebase --onto feature-1 feature-1@{1} feature-2b. The expression feature-1@{1} names the previous position of feature-1, so only the commits unique to feature-2b are replayed onto the rewritten feature-1.