Sub topic 6
Making --update-refs the default
This chapter assumes you have used git rebase --update-refs on a stack, as in the previous chapters, and that you have Git 2.38 or newer. It covers how to turn the option on permanently, how to override it, and the surprises that come with an always-on setting.
Why make it a default
Rebasing a stack without --update-refs leaves the lower branches behind. You then have to move them by hand. If you work with stacks regularly, you will forget the flag at some point. The result is a stack where feature-3 sits on the new base and feature-1 and feature-2 still point at old commits.
The rebase.updateRefs setting removes that failure mode. When it is true, every git rebase behaves as if you had typed --update-refs. Branches that point into the rewritten range are moved to their rewritten commits, and the rest of your repository is left alone.
The setting does not change what --update-refs does. It only changes whether you have to ask for it.
Turning it on
Set it once for your user account:
git config --global rebase.updateRefs trueThis writes a line to ~/.gitconfig:
[rebase]
updateRefs = trueYou can also set it for a single repository by dropping --global:
git config rebase.updateRefs trueA repository setting beats a global one, so you can enable it everywhere and disable it in one repository, or the reverse. To turn the repository-level value off while the global value stays on:
git config rebase.updateRefs falseTo remove a setting entirely so the next level down applies:
git config --global --unset rebase.updateRefsWithout any setting, the default is false. This is why a fresh Git install, even a recent one, does not move your intermediate branches.
Overriding it for one command
The command-line flag always wins over configuration. With the setting on, you can still skip the behavior for one rebase:
git rebase --no-update-refs mainThe opposite also works. With the setting off, --update-refs turns it on for a single run:
git rebase --update-refs origin/mainThe precedence is:
- The command-line flag,
--update-refsor--no-update-refs. - The repository setting in
.git/config. - The global setting in
~/.gitconfig. - The built-in default, which is off.
Checking what is in effect
When a rebase behaves unexpectedly, find out which configuration file supplies the value:
git config --show-origin --get rebase.updateRefsThe output names the file and the value, for example file:/home/ana/.gitconfig true. If the command prints nothing and exits with status 1, the setting is not defined anywhere and the default of off applies.
The quickest behavioral check is an interactive rebase. With the setting on, the todo list contains update-ref lines for every branch that points into the range:
git switch feature-3
git rebase -i origin/mainYou should see lines like these in the editor:
pick 1a2b3c4 Add parser skeleton
update-ref refs/heads/feature-1
pick 5d6e7f8 Add parser tests
pick 9a8b7c6 Wire parser into CLI
update-ref refs/heads/feature-2
pick 0f1e2d3 Document CLI flagIf the lines are missing, the setting is not active. Another possibility is that no other branch points into the range.
Where the setting applies
The setting applies to any command that runs git rebase. That includes some commands you may not think of as rebases:
git rebaseandgit rebase -i, the obvious cases.git pull --rebase, orgit pullwithpull.rebaseset. Pull runs a rebase internally, so branches that point at your local commits being replayed are moved too.git rebase --onto, which still honors the setting.
The pull --rebase case can surprise you. Suppose you are on feature-3 and pull new commits from its upstream. Without the setting, feature-1 and feature-2 stay behind. With it, they move along with feature-3, which is usually what you want in a stack.
What the setting does not change
The setting is a convenience, not a bypass of the safety rules. These limits hold with or without it:
- A branch checked out in another worktree is not updated. Git will not move a branch out from under a working directory that has it checked out.
- Only branches that point at commits inside the range being rewritten are moved. A branch pointing at the base commit or at commits outside the range stays where it is.
- The rewritten commits get new IDs, so every moved branch needs a force push afterward. Use
--force-with-lease, as covered in the push chapter. - The setting does not resolve conflicts. A conflict in the middle of a stack stops the rebase as usual, and the branches after the conflict point have not yet been moved.
Common errors and surprises
| Symptom | Likely cause | Fix |
|---|---|---|
| Intermediate branches are still on old commits | The setting is off, or Git is older than 2.38 | Run `git --version` and `git config --show-origin --get rebase.updateRefs` |
| A branch you did not intend to touch moved | It pointed into the rebased range, for example a backup or scratch branch | Restore it from the reflog and keep backups as tags |
| `unknown option` error for `--update-refs` | Git older than 2.38 | Upgrade Git |
| A stack branch did not move | It is checked out in another worktree | Switch that worktree to another branch, then redo the rebase |
| A teammate's rebase behaves differently from yours | Their configuration differs from yours | Compare `git config --list --show-origin` on both machines |
| Rebase in one repository ignores your global setting | A repository setting overrides it | Check `.git/config` for `updateRefs = false` |
The backup branch trap
Many people protect themselves before a risky rebase with a backup branch:
git branch backup/feature-3 feature-3
git rebase origin/mainWithout --update-refs, the backup stays put. With the setting on, backup/feature-3 points at the tip of the range, so Git treats it as an intermediate branch and rewrites it along with everything else. The backup then no longer holds your old history.
Use a tag instead. Tags are not moved by --update-refs, which only handles branches:
git tag backup-feature-3 feature-3
git rebase origin/mainYou can delete the tag once the rebase is done and checked. The reflog also keeps the old positions, which is the better recovery tool, and a later chapter covers it.
Should you turn it on?
For people who keep stacks, yes. The downside is small: occasionally a branch moves that you wanted to leave alone, and that is recoverable. The upside is that you cannot forget the flag.
If you only occasionally stack branches and mostly rebase single branches, leaving the setting off is also reasonable. A single-branch rebase gives the same result either way, because no other branch points into the range.
A compromise is to enable it per repository for the projects where you stack, and leave the global default alone:
cd ~/work/parser-project
git config rebase.updateRefs trueVerifying the effect end to end
This sequence shows the setting working. It assumes feature-1, feature-2, and feature-3 form a stack on main, and that you have fetched:
git config rebase.updateRefs true
git fetch origin
git switch feature-3
git rebase origin/main
git log --oneline --decorate --graph origin/main..feature-3The log should show all three branch names, each on its own rewritten commit, directly above origin/main. If feature-1 or feature-2 is missing from the decorations, the setting did not apply. Check the configuration as described earlier, then compare git rev-parse feature-1 before and after to see whether the branch moved.
Exercise
You enabled rebase.updateRefs globally. You keep a branch named scratch/old-attempt that points at the middle commit of your current stack, and you want it to stay exactly where it is. After running git rebase origin/main on the top branch, you find it has moved. Give two ways to prevent that, and say how you would restore it now.
Show answer
To prevent it, either run the rebase with git rebase --no-update-refs origin/main, or store the bookmark as a tag instead of a branch, because tags are not moved by --update-refs. To restore the branch now, find its earlier position in the reflog with git reflog show scratch/old-attempt and reset it with git branch -f scratch/old-attempt 'scratch/old-attempt@{1}'.
Exercise
A colleague says git config --global rebase.updateRefs true has no effect on their machine. Their git --version prints 2.36. A repository-level updateRefs = false is not set. What is the cause, and what would you check after fixing it?
Show answer
Git 2.36 predates --update-refs and the rebase.updateRefs key, which arrived in 2.38, so the setting is ignored. After upgrading Git, run git config --show-origin --get rebase.updateRefs to confirm the value is read, then run git rebase -i on a stack and check that the todo list contains update-ref lines.