Sub topic 6
Making --update-refs the default
This chapter assumes you can rebase a single branch, know that a branch is a pointer to a commit, and have Git 2.38 or newer installed. Earlier chapters showed that git rebase --update-refs moves every branch in a stack. Typing the flag each time is easy to forget. Forgetting it leaves the lower branches behind on old commits.
The rebase.updateRefs setting
Git has a configuration key, rebase.updateRefs, that turns the option on for every rebase you run. Set it once and plain git rebase behaves as if you had passed --update-refs.
git config --global rebase.updateRefs trueThe --global flag writes the value to your user-level config file. Omit it to set the value only in the current repository. Check what Git will use with:
git config --get rebase.updateRefsThe command prints true when the setting is active. It prints nothing if the key is unset.
Overriding the default for one command
Sometimes you want a rebase that touches only the current branch. The command-line option always wins over the config value, so --no-update-refs turns the behavior off for that one command.
git rebase --no-update-refs mainThe next rebase you run without the flag uses the config default again.
What changes when it is on
With the setting on, every rebase checks whether other local branches point at commits that the rebase is about to rewrite. If they do, Git moves those branches to the rewritten commits at the end. In the stack from earlier chapters, this command now keeps all three branches in step:
git fetch origin
git switch feature-3
git rebase origin/mainYou no longer need the flag. After the rebase finishes, feature-1 and feature-2 point at their new commits, not the old ones.
In an interactive rebase (git rebase -i), the update-ref lines also appear in the todo list without any flag, so you can read and edit them as before.
Common errors
- The setting seems ignored. Check the Git version with
git --version. Versions before 2.38 do not know the key and silently skip it. - A branch did not move. Git does not update a branch that is checked out in another worktree. The next chapter covers this limit.
- A repository behaves differently from others. A repository-level value overrides your global one. Run
git config --show-origin --get rebase.updateRefsto see which file supplies the value. - A rebase rewrites a branch you did not expect. The setting applies to any local branch that points into the rebased range, not only branches you think of as part of the stack. Run
git branch --contains <commit>before a rebase if you are unsure.
Summary of the options
| Setting | Effect on a plain `git rebase` |
|---|---|
| `rebase.updateRefs` unset or `false` | Only the current branch moves |
| `rebase.updateRefs` set to `true` | Every stacked local branch moves |
| `--update-refs` on the command line | Every stacked local branch moves |
| `--no-update-refs` on the command line | Only the current branch moves, whatever the config says |
Exercise
You set rebase.updateRefs to true globally. You want one rebase of feature-3 onto main that leaves feature-1 and feature-2 untouched. What command do you run, and why does it work?
Show answer
Run git rebase --no-update-refs main from feature-3. A command-line option takes precedence over the config value, so Git moves only the current branch for this one command. Later rebases use the config default again.
Exercise
A teammate says the setting is on, but after their rebase feature-1 still points at an old commit. Name two things to check.
Show answer
First, check the Git version with git --version, since versions before 2.38 ignore the key. Second, check whether feature-1 is checked out in another worktree, because Git does not move a branch checked out elsewhere. Running git config --show-origin --get rebase.updateRefs also shows whether a repository-level value overrides the global one.