日本語

How to Rebase Stacked Git Branches Together with –update-refs

​​Git​command line

When branches are stacked on top of each other, running git rebase --update-refs main on the topmost branch also rebases the branches below it onto the new main. This is for the workflow where you split a large change into small pull requests, create each branch from the previous one, and have them reviewed in order. Use it after main has moved ahead. Below, I look at what happens without the flag, how a conflict during the rebase behaves, and how to turn it on by default with a config setting, using output from a test environment.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and I fixed the commit dates with environment variables so the output would be the same every time. I ran git rebase --continue with GIT_EDITOR=true so that it uses the existing message without opening an editor. For git rebase -i, I set the environment variable GIT_SEQUENCE_EDITOR to a small shell script that only displays the todo list without changing it. The commands in the body omit these environment variables. The push target is a bare repository I created in the same test environment, referenced by the relative path ../remote.git. I did not test GitHub or other hosting services, combinations with --rebase-merges, or Windows and macOS.

The Three-Branch Example

I created feat-a from main and added two commits to it. Then I created feat-b on top of feat-a, and feat-c on top of feat-b. In the meantime, main received a commit, "write the address". I left the repository with feat-c checked out, the topmost branch.

console
$ git log --oneline --graph --all
* d078b6b 住所を書く
| * 9fe57a3 C: 定休日を書く
| * ca8803d B: 営業時間を書く
| * a05affc A: みそ汁を追加
| * b0ceb6e A: おにぎりを追加
|/  
* 40ae69a 最初のコミット

feat-a points to a05affc, feat-b to ca8803d, and feat-c to 9fe57a3.

Without –update-refs, the middle branches stay in the old place

On feat-c, I ran git rebase main as usual.

console
$ git rebase main
Rebasing (1/4)
Rebasing (2/4)
Rebasing (3/4)
Rebasing (4/4)

Successfully rebased and updated refs/heads/feat-c.

$ git branch -v
  feat-a a05affc A: みそ汁を追加
  feat-b ca8803d B: 営業時間を書く
* feat-c a74c97b C: 定休日を書く
  main   d078b6b 住所を書く

All four commits were recreated on top of the new main, and feat-c moved to the new tip a74c97b. However, feat-a and feat-b still point to a05affc and ca8803d, the same commits as before the rebase. The commits for feat-a and feat-b were recreated as part of the history of feat-c with new hashes, but the branch names still point at the old commits, which do not include the "write the address" commit from main.

With –update-refs, the middle branches move too

I reset back to the state before the rebase with git reset --hard, then ran the command with --update-refs.

console
$ git rebase --update-refs main
Rebasing (1/6)
Rebasing (2/6)
Rebasing (3/6)
Rebasing (4/6)
Rebasing (5/6)
Rebasing (6/6)

Successfully rebased and updated refs/heads/feat-c.
Updated the following refs with --update-refs:
	refs/heads/feat-a
	refs/heads/feat-b

$ git branch -v
  feat-a 4d8ec04 A: みそ汁を追加
  feat-b f430a94 B: 営業時間を書く
* feat-c dd880e9 C: 定休日を書く
  main   d078b6b 住所を書く

The last lines say Updated the following refs with --update-refs:, and feat-a and feat-b moved to the recreated commits as well. git log --oneline --graph --all shows the four commits in a single line on top of the new main, with feat-a on the second commit and feat-b on the third.

A diagram showing three histories of the three stacked branches, arranged one above the other: before the rebase, after a rebase without --update-refs, and after a rebase with it. Without the flag, only feat-a and feat-b stay on the old commits. With the flag, all three move to the new commits.
With and without –update-refs, feat-a and feat-b end up in different places (hashes are from the test output)

The official git rebase reference describes --update-refs as automatically force-updating branches that point to the commits being rebased. The reflogs of feat-a and feat-b record the rewrite done by the rebase.

console
$ git reflog -n 2 feat-a
4d8ec04 feat-a@{0}: rewritten during rebase
a05affc feat-a@{1}: commit: A: みそ汁を追加

feat-a@{1} is a05affc, the commit from before the rebase, so you can use it to go back.

–update-refs adds update-ref lines to the todo list

The progress count changes from (1/4) without the flag to (1/6) with it. You can see what was added by looking at the todo list that opens with git rebase -i --update-refs main.

console
$ git rebase -i --update-refs main
pick b0ceb6e A: おにぎりを追加
pick a05affc A: みそ汁を追加
update-ref refs/heads/feat-a

pick ca8803d B: 営業時間を書く
update-ref refs/heads/feat-b

pick 9fe57a3 C: 定休日を書く

# Rebase d078b6b..9fe57a3 onto d078b6b (6 commands)
(中略)
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase

An update-ref line is inserted right after the pick for the commit that a branch points to. There are four pick lines and two update-ref lines, for six in total. As the todo list's description says, the branches are updated all at once at the end of the rebase, to the recreated commit at the position where their update-ref line appeared.

The official list of options that cannot be combined says --update-refs cannot be used with --apply. A plain git rebase uses the default --merge backend, so it works without problems.

Rebasing the upper branches after fixing a lower one

The same command also brings the upper branches along when you add a commit to a lower branch later. I committed "add pickled radish" to feat-a, switched back to feat-c, and rebased onto feat-a instead of main.

console
$ git rebase --update-refs feat-a
Rebasing (1/3)
Rebasing (2/3)
Rebasing (3/3)

Successfully rebased and updated refs/heads/feat-c.
Updated the following refs with --update-refs:
	refs/heads/feat-b

$ git log --oneline --graph --all
* 6bcfafc C: 定休日を書く
* 87d1d47 B: 営業時間を書く
* 709a348 A: レビューの指摘でたくあんを追加
* 503dd15 A: みそ汁を追加
* 7f480fb A: おにぎりを追加
* d078b6b 住所を書く
(以下、rebase の前の古いコミットなどは省略)

The rebase replayed the two commits B and C, and feat-b moved to the new location as well. feat-a did not move, because it is the target of the rebase.

Conflicts during the rebase, and –abort

I committed a file hours.md (containing "hours: undecided") to main, then ran git rebase --update-refs main. The rebase stopped with a conflict at the feat-b commit, which creates the same file. Here is git branch -v at the point where it stopped.

console
$ git branch -v
* (no branch, rebasing feat-c) e4c8b21 A: レビューの指摘でたくあんを追加
  feat-a                       709a348 A: レビューの指摘でたくあんを追加
  feat-b                       87d1d47 B: 営業時間を書く
  feat-c                       6bcfafc C: 定休日を書く
  main                         2ac48f5 営業時間の欄を作る

In git status, the update-ref line for feat-a appeared in the section of completed steps. Even so, feat-a still points to 709a348, the commit it had before the rebase started. Branches are updated at the very end of the rebase, so if you run git rebase --abort at this point, all three branches stay where they were before you started.

console
$ git rebase --abort

$ git branch -v
  feat-a 709a348 A: レビューの指摘でたくあんを追加
  feat-b 87d1d47 B: 営業時間を書く
* feat-c 6bcfafc C: 定休日を書く
  main   2ac48f5 営業時間の欄を作る

I ran the rebase again. This time I changed hours.md to "hours: from 11:00", ran git add, and continued with git rebase --continue. When the rebase finished, feat-a and feat-b were updated together.

console
$ printf "営業時間: 11時から\n" > hours.md && git add hours.md && git rebase --continue
[detached HEAD 973f54f] B: 営業時間を書く
 1 file changed, 1 insertion(+), 1 deletion(-)
Rebasing (6/7)
Rebasing (7/7)

Successfully rebased and updated refs/heads/feat-c.
Updated the following refs with --update-refs:
	refs/heads/feat-a
	refs/heads/feat-b

$ git branch -v
  feat-a 9b0933e A: レビューの指摘でたくあんを追加
  feat-b 973f54f B: 営業時間を書く
* feat-c 3b9d127 C: 定休日を書く
  main   2ac48f5 営業時間の欄を作る

feat-b points to 973f54f, the commit recreated after resolving the conflict.

Turning it on for every rebase with rebase.updateRefs

According to the documentation for the rebase.updateRefs setting, setting it to true makes --update-refs the default. After setting it, I ran git rebase main without any options.

console
$ git config rebase.updateRefs true

$ git rebase main
(中略)
Successfully rebased and updated refs/heads/feat-c.
Updated the following refs with --update-refs:
	refs/heads/feat-a
	refs/heads/feat-b

Even without the option, feat-a and feat-b were updated. If you want to skip it for a single rebase while the setting is on, pass --no-update-refs. With the setting still true, running git rebase --no-update-refs main did not print any Updated the following refs lines, and feat-a and feat-b stayed where they were. In the example above, I did not pass --global to git config, so the setting applies only to this repository.

Pushing the moved branches together

I pushed the three branches first, then added a commit to main and rebased with git rebase --update-refs main. Since I had rebased branches that were already pushed, the push needed a force push. Listing all the branch names in one command, a single git push --force-with-lease sent them all.

console
$ git push --force-with-lease origin feat-a feat-b feat-c
To ../remote.git
 + b10673a...19506ff feat-a -> feat-a (forced update)
 + 09f04f5...f895779 feat-b -> feat-b (forced update)
 + e9edae7...2505171 feat-c -> feat-c (forced update)

The official git push reference for --force-with-lease says that when you give it no value, it overwrites the remote branch only if the remote branch is at the same commit as your remote-tracking branch (such as origin/feat-a). Otherwise, it refuses the push.

Branches checked out in another worktree do not move

The official reference also says that branches checked out in a worktree are not updated by this method. I added a commit "write the number of seats" to main, then opened feat-b in another folder with git worktree add and tested it that way.

console
$ git worktree add -q ../wt-b feat-b

$ git rebase --update-refs main
(中略)
Successfully rebased and updated refs/heads/feat-c.
Updated the following refs with --update-refs:
	refs/heads/feat-a

$ git branch -v
  feat-a dd9365a A: レビューの指摘でたくあんを追加
+ feat-b f895779 B: 営業時間を書く
* feat-c 3904849 C: 定休日を書く
  main   5be9b66 席数を書く

Only feat-a was updated, and feat-b stayed on the old commit. git branch -v puts a + at the start of the line for a branch checked out in another worktree. In git log --oneline --graph feat-b feat-c, feat-b is still on the branch from before the rebase, while feat-c contains the recreated B commit (eb31643). When you use worktrees, check the branch names listed under Updated the following refs to make sure none of the branches you expected to move were left behind.

​Posted by 86