Volume XXII, number 279Tuesday, October 6, 2026Latest message 13 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patch, 2 partsrebase: handle --update-refs branch symrefs

25 messages between May 28, 2026 and Aug 7, 2026, from Son Luong Ngoc via GitGitGadget, Kristoffer Haugsbakk, Junio C Hamano, Phillip Wood, Son Luong Ngoc, Erik Cervin-Edin.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

Son Luong Ngoc via GitGitGadgetMay 28, 2026, 05:41 UTC on lore

git rebase --update-refs can fail after the normal rebase path has successfully updated the current branch when another local branch is a symbolic ref to it.

One practical way to arrive at that setup is a default branch rename from master to main. While the migration is in progress, a user may keep refs/heads/main as a symbolic ref to refs/heads/master so that both names continue to work locally.

If pull.rebase is enabled, a plain git pull can then finish the rebase of master and still fail while trying to update the main alias. The reported failure looked like this, with line breaks adjusted for the cover letter:

Successfully rebased and updated refs/heads/master.
error: update_ref failed for ref 'refs/heads/main':
cannot lock ref 'refs/heads/main':
is at fc2c7bd5f17abec7861ef759edcd33a1e16662a1
but expected 531cabdfb49098d6ffa502ed4bf91d1b35edfcfa
Updated the following refs with --update-refs:
Failed to update the following refs with --update-refs:
        refs/heads/main

The sequencer builds its update-ref todo commands from local branch decorations. It excludes the current branch by comparing the literal decoration name with the resolved HEAD ref, so refs/heads/main is not recognized as the current branch alias. The final update then dereferences the alias back to master, but master has already moved, so the old-OID check fails.

This series keeps that failure mode explicit by adding a known-breakage test first, so that the behavioral expectation is clear before the fix. The fix then resolves local branch symref decorations before queuing update-ref commands, skips aliases of the current branch, and skips duplicate referents. That keeps the existing old-OID protection on the single real branch update. It also avoids teaching the final ref update step to treat this race-looking mismatch as success.

Patch overview:
 * Patch 1 records the branch-symref failure in t3404.
 * Patch 2 flips the test and canonicalizes sequencer behavior.
Son Luong Ngoc (2):
  t3404: add failing branch symref test
  rebase: skip branch symref aliases
 sequencer.c                   | 63 +++++++++++++++++++++++++++++------
 t/t3404-rebase-interactive.sh | 25 ++++++++++++++
 2 files changed, 77 insertions(+), 11 deletions(-)
base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2126%2Fsluongng%2Fsl%2Frebase-update-refs-symrefs-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2126/sluongng/sl/rebase-update-refs-symrefs-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2126
-- 
gitgitgadget
Son Luong Ngoc via GitGitGadgetMay 28, 2026, 05:42 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH 1/2] t3404: add failing branch symref test

From: Son Luong Ngoc <sluongng@gmail.com>

rebase --update-refs queues local branch decorations by their literal refnames. When a branch such as refs/heads/main is a symbolic ref to the current branch, the normal rebase path first updates the current branch and the queued symref update later tries to update the same referent with the old value it recorded before the rebase.

Add a known-breakage test that exercises this case so that the fix can flip it to test_expect_success. The expected behavior is that the branch symref keeps pointing at the rebased current branch.

Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
---
 t/t3404-rebase-interactive.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
Show changes to t/t3404-rebase-interactive.sh +25 −0
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 58b3bb0c27..42ba8cc313 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1978,6 +1978,31 @@ test_expect_success '--update-refs ignores non-branch decorations' '
 	test_cmp expect actual
 '
 
+test_expect_failure '--update-refs skips branch symrefs to current branch' '
+	test_when_finished "
+		test_might_fail git rebase --abort &&
+		git checkout primary &&
+		test_might_fail git symbolic-ref -d refs/heads/update-refs-symref-alias &&
+		test_might_fail git branch -D update-refs-symref update-refs-symref-base
+	" &&
+	git checkout -B update-refs-symref-base primary &&
+	test_commit --no-tag update-refs-symref-base symref-base.t &&
+	git checkout -B update-refs-symref &&
+	test_commit --no-tag update-refs-symref-topic symref-topic.t &&
+	git checkout update-refs-symref-base &&
+	test_commit --no-tag update-refs-symref-newbase symref-newbase.t &&
+	git checkout update-refs-symref &&
+	git symbolic-ref refs/heads/update-refs-symref-alias refs/heads/update-refs-symref &&
+
+	git rebase --update-refs update-refs-symref-base 2>err &&
+
+	test_cmp_rev update-refs-symref-base update-refs-symref^ &&
+	test_cmp_rev refs/heads/update-refs-symref refs/heads/update-refs-symref-alias &&
+	test_write_lines refs/heads/update-refs-symref >expect &&
+	git symbolic-ref refs/heads/update-refs-symref-alias >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success '--update-refs updates refs correctly' '
 	git checkout -B update-refs no-conflict-branch &&
 	git branch -f base HEAD~4 &&
-- 
gitgitgadget
Son Luong Ngoc via GitGitGadgetMay 28, 2026, 05:42 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH 2/2] rebase: skip branch symref aliases

From: Son Luong Ngoc <sluongng@gmail.com>

rebase --update-refs records local branch decorations before replaying commits. If a decoration is a symbolic branch such as refs/heads/main pointing at refs/heads/master, updating it later dereferences back to master and can fail because the normal rebase path already moved that branch.

Resolve local branch symref decorations to their referents before queuing update-ref commands, and skip duplicates. This keeps branch aliases from scheduling a second update for the same underlying branch while still using the existing old-OID check for the single queued update.

Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
---
 sequencer.c                   | 63 +++++++++++++++++++++++++++++------
 t/t3404-rebase-interactive.sh |  2 +-
 2 files changed, 53 insertions(+), 12 deletions(-)
Show changes to 2 files +53 −12

sequencer.c, t/t3404-rebase-interactive.sh

diff --git a/sequencer.c b/sequencer.c
index 1ee4b2875b..4a83d1337c 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6445,15 +6445,22 @@ static int add_decorations_to_list(const struct commit *commit,
 				   struct todo_add_branch_context *ctx)
 {
 	const struct name_decoration *decoration = get_name_decoration(&commit->object);
-	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
-						       "HEAD",
+	struct ref_store *refs = get_main_ref_store(the_repository);
+	const char *head_ref = refs_resolve_ref_unsafe(refs, "HEAD",
 						       RESOLVE_REF_READING,
-						       NULL,
-						       NULL);
+						       NULL, NULL);
+	char *resolved_head_ref = refs_resolve_refdup(refs, "HEAD",
+						       RESOLVE_REF_READING,
+						       NULL, NULL);
+	struct strbuf update_ref = STRBUF_INIT;
 
 	while (decoration) {
 		struct todo_item *item;
 		const char *path;
+		const char *ref = decoration->name;
+		const char *resolved_ref;
+		int is_symref = 0;
+		int flags = 0;
 		size_t base_offset = ctx->buf->len;
 
 		/*
@@ -6461,12 +6468,44 @@ static int add_decorations_to_list(const struct commit *commit,
 		 * updated by the default rebase behavior.
 		 * Exclude it from the list of refs to update,
 		 * as well as any non-branch decorations.
+		 *
+		 * Resolve branch symrefs after checking for the current HEAD so
+		 * that aliases do not schedule duplicate updates for their
+		 * referents.
+		 *
 		 * Non-branch decorations may be present if the pretty format
 		 * includes "%d", which would have loaded all refs
 		 * into the global decoration table.
 		 */
-		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
-		    (decoration->type != DECORATION_REF_LOCAL)) {
+		if (decoration->type != DECORATION_REF_LOCAL) {
+			decoration = decoration->next;
+			continue;
+		}
+
+		if (head_ref && !strcmp(head_ref, ref)) {
+			decoration = decoration->next;
+			continue;
+		}
+
+		strbuf_reset(&update_ref);
+		resolved_ref = refs_resolve_ref_unsafe(refs, ref,
+						       RESOLVE_REF_READING |
+						       RESOLVE_REF_NO_RECURSE,
+						       NULL, &flags);
+		if ((flags & REF_ISSYMREF) && resolved_ref) {
+			if (!starts_with(resolved_ref, "refs/heads/")) {
+				decoration = decoration->next;
+				continue;
+			}
+
+			strbuf_addstr(&update_ref, resolved_ref);
+			ref = update_ref.buf;
+			is_symref = 1;
+		}
+
+		if ((is_symref && resolved_head_ref &&
+		     !strcmp(resolved_head_ref, ref)) ||
+		    string_list_has_string(&ctx->refs_to_oids, ref)) {
 			decoration = decoration->next;
 			continue;
 		}
@@ -6478,19 +6517,19 @@ static int add_decorations_to_list(const struct commit *commit,
 		memset(item, 0, sizeof(*item));
 
 		/* If the branch is checked out, then leave a comment instead. */
-		if ((path = branch_checked_out(decoration->name))) {
+		if ((path = branch_checked_out(ref))) {
 			item->command = TODO_COMMENT;
 			strbuf_commented_addf(ctx->buf, comment_line_str,
 					      "Ref %s checked out at '%s'\n",
-					      decoration->name, path);
+					      ref, path);
 		} else {
 			struct string_list_item *sti;
 			item->command = TODO_UPDATE_REF;
-			strbuf_addf(ctx->buf, "%s\n", decoration->name);
+			strbuf_addf(ctx->buf, "%s\n", ref);
 
 			sti = string_list_insert(&ctx->refs_to_oids,
-						 decoration->name);
-			sti->util = init_update_ref_record(decoration->name);
+						 ref);
+			sti->util = init_update_ref_record(ref);
 		}
 
 		item->offset_in_buf = base_offset;
@@ -6501,6 +6540,8 @@ static int add_decorations_to_list(const struct commit *commit,
 		decoration = decoration->next;
 	}
 
+	strbuf_release(&update_ref);
+	free(resolved_head_ref);
 	return 0;
 }
 
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 42ba8cc313..29447c0fc3 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1978,7 +1978,7 @@ test_expect_success '--update-refs ignores non-branch decorations' '
 	test_cmp expect actual
 '
 
-test_expect_failure '--update-refs skips branch symrefs to current branch' '
+test_expect_success '--update-refs skips branch symrefs to current branch' '
 	test_when_finished "
 		test_might_fail git rebase --abort &&
 		git checkout primary &&
-- 
gitgitgadget
Kristoffer HaugsbakkMay 28, 2026, 07:08 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH 2/2] rebase: skip branch symref aliases

On Thu, May 28, 2026, at 07:42, Son Luong Ngoc via GitGitGadget wrote:
Show 6 quoted lines
>[snip]
> -test_expect_failure '--update-refs skips branch symrefs to current branch' '
> +test_expect_success '--update-refs skips branch symrefs to current branch' '
>  	test_when_finished "
>  		test_might_fail git rebase --abort &&
>  		git checkout primary &&
This style of fixing a bug by:

• Add failing test `test_expect_failure` in the first commit • Fix the bug in the next commit and flip to `test_expect_success`

Is legitimate and makes it easier to verify that the test really exercises the regression. But in this project it is preferred to just do the bug fix + regression test in one patch.

See https://lore.kernel.org/git/xmqqfrdk3aqy.fsf@gitster.g/
> --
> gitgitgadget
Junio C HamanoMay 28, 2026, 20:42 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH 0/2] rebase: handle --update-refs branch symrefs

"Son Luong Ngoc via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 21 quoted lines
> git rebase --update-refs can fail after the normal rebase path has
> successfully updated the current branch when another local branch is a
> symbolic ref to it.
>
> One practical way to arrive at that setup is a default branch rename from
> master to main. While the migration is in progress, a user may keep
> refs/heads/main as a symbolic ref to refs/heads/master so that both names
> continue to work locally.
>
> If pull.rebase is enabled, a plain git pull can then finish the rebase of
> master and still fail while trying to update the main alias. The reported
> failure looked like this, with line breaks adjusted for the cover letter:
>
> Successfully rebased and updated refs/heads/master.
> error: update_ref failed for ref 'refs/heads/main':
> cannot lock ref 'refs/heads/main':
> is at fc2c7bd5f17abec7861ef759edcd33a1e16662a1
> but expected 531cabdfb49098d6ffa502ed4bf91d1b35edfcfa
> Updated the following refs with --update-refs:
> Failed to update the following refs with --update-refs:
>         refs/heads/main

I vaguely recall we saw a different topic that dealt with a situation somewhat similar to this topic (I think it was about 'describe' giving a name that is not a branch). How would this mesh with what the other topic wanted to do? Instead of filtering out non-branch names (which the other topic did), here we want to filter out names that are not concrete branches but pointers to something else. Would it mean that the logic here is more broad (i.e., both wants to filter out names of non-branches), making the other topic unnecessary?

Phillip WoodJun 1, 2026, 13:52 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH 1/2] t3404: add failing branch symref test

On 28/05/2026 06:42, Son Luong Ngoc via GitGitGadget wrote:
Show 11 quoted lines
> From: Son Luong Ngoc <sluongng@gmail.com>
> 
> rebase --update-refs queues local branch decorations by their literal
> refnames. When a branch such as refs/heads/main is a symbolic ref to
> the current branch, the normal rebase path first updates the current
> branch and the queued symref update later tries to update the same
> referent with the old value it recorded before the rebase.
> 
> Add a known-breakage test that exercises this case so that the fix can
> flip it to test_expect_success. The expected behavior is that the branch
> symref keeps pointing at the rebased current branch.

Thanks for adding a test, I'd find it easier to review this series if the test was added in the same patch as the fix which is our usual practice.

Show 15 quoted lines
> +test_expect_failure '--update-refs skips branch symrefs to current branch' '
> +	test_when_finished "
> +		test_might_fail git rebase --abort &&
> +		git checkout primary &&
> +		test_might_fail git symbolic-ref -d refs/heads/update-refs-symref-alias &&
> +		test_might_fail git branch -D update-refs-symref update-refs-symref-base
> +	" &&
> +	git checkout -B update-refs-symref-base primary &&
> +	test_commit --no-tag update-refs-symref-base symref-base.t &&
> +	git checkout -B update-refs-symref &&
> +	test_commit --no-tag update-refs-symref-topic symref-topic.t &&
> +	git checkout update-refs-symref-base &&
> +	test_commit --no-tag update-refs-symref-newbase symref-newbase.t &&
> +	git checkout update-refs-symref &&
> +	git symbolic-ref refs/heads/update-refs-symref-alias refs/heads/update-refs-symref &&

I think we want to test a symref that does not match HEAD as well. Rather than adding a new test, can we instead add a couple of symref branches to the test "--update-refs updates refs correctly"?

Thanks
Phillip
Show 13 quoted lines
> +
> +	git rebase --update-refs update-refs-symref-base 2>err &&
> +
> +	test_cmp_rev update-refs-symref-base update-refs-symref^ &&
> +	test_cmp_rev refs/heads/update-refs-symref refs/heads/update-refs-symref-alias &&
> +	test_write_lines refs/heads/update-refs-symref >expect &&
> +	git symbolic-ref refs/heads/update-refs-symref-alias >actual &&
> +	test_cmp expect actual
> +'
> +
>   test_expect_success '--update-refs updates refs correctly' '
>   	git checkout -B update-refs no-conflict-branch &&
>   	git branch -f base HEAD~4 &&
Phillip WoodJun 1, 2026, 14:10 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH 2/2] rebase: skip branch symref aliases

On 28/05/2026 06:42, Son Luong Ngoc via GitGitGadget wrote:
Show 7 quoted lines
> From: Son Luong Ngoc <sluongng@gmail.com>
> 
> rebase --update-refs records local branch decorations before replaying
> commits. If a decoration is a symbolic branch such as refs/heads/main
> pointing at refs/heads/master, updating it later dereferences back to
> master and can fail because the normal rebase path already moved that
> branch.
Good explanation, thanks for working on this.
> Resolve local branch symref decorations to their referents before
s/referents/targets/ ?
> queuing update-ref commands, and skip duplicates. This keeps branch
> aliases from scheduling a second update for the same underlying branch
> while still using the existing old-OID check for the single queued
> update.

That's not quite what the patch does though - it only checks that the target of the symref differs from the target of HEAD. If a symref points to another branch we still try to update it when we should skip it.

Show 25 quoted lines
> Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
> ---
>   sequencer.c                   | 63 +++++++++++++++++++++++++++++------
>   t/t3404-rebase-interactive.sh |  2 +-
>   2 files changed, 53 insertions(+), 12 deletions(-)
> 
> diff --git a/sequencer.c b/sequencer.c
> index 1ee4b2875b..4a83d1337c 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -6445,15 +6445,22 @@ static int add_decorations_to_list(const struct commit *commit,
>   				   struct todo_add_branch_context *ctx)
>   {
>   	const struct name_decoration *decoration = get_name_decoration(&commit->object);
> -	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
> -						       "HEAD",
> +	struct ref_store *refs = get_main_ref_store(the_repository);
> +	const char *head_ref = refs_resolve_ref_unsafe(refs, "HEAD",
>   						       RESOLVE_REF_READING,
> -						       NULL,
> -						       NULL);
> +						       NULL, NULL);
> +	char *resolved_head_ref = refs_resolve_refdup(refs, "HEAD",
> +						       RESOLVE_REF_READING,
> +						       NULL, NULL);

We need to use refs_resolve_refdup() instead of refs_resolve_ref_unsafe() so that the return value is not overwritten by the later calls to refs_resolve_ref_unsafe() that are added below. But that is the only change that is needed - we do not need to add a new variable, we just replace refs_resolve_ref_unsafe() with refs_resole_refdup() and free "head_ref" before we return.

Show 36 quoted lines
> +	struct strbuf update_ref = STRBUF_INIT;
>   
>   	while (decoration) {
>   		struct todo_item *item;
>   		const char *path;
> +		const char *ref = decoration->name;
> +		const char *resolved_ref;
> +		int is_symref = 0;
> +		int flags = 0;
>   		size_t base_offset = ctx->buf->len;
>   
>   		/*
> @@ -6461,12 +6468,44 @@ static int add_decorations_to_list(const struct commit *commit,
>   		 * updated by the default rebase behavior.
>   		 * Exclude it from the list of refs to update,
>   		 * as well as any non-branch decorations.
> +		 *
> +		 * Resolve branch symrefs after checking for the current HEAD so
> +		 * that aliases do not schedule duplicate updates for their
> +		 * referents.
> +		 *
>   		 * Non-branch decorations may be present if the pretty format
>   		 * includes "%d", which would have loaded all refs
>   		 * into the global decoration table.
>   		 */
> -		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
> -		    (decoration->type != DECORATION_REF_LOCAL)) {
> +		if (decoration->type != DECORATION_REF_LOCAL) {
> +			decoration = decoration->next;
> +			continue;
> +		}
> +
> +		if (head_ref && !strcmp(head_ref, ref)) {
> +			decoration = decoration->next;
> +			continue;
> +		}

This is just rewriting the existing if statement which has nothing to do with the stated aim of this patch - lets leave it as it was.

Show 5 quoted lines
> +
> +		strbuf_reset(&update_ref);
> +		resolved_ref = refs_resolve_ref_unsafe(refs, ref,
> +						       RESOLVE_REF_READING |
> +						       RESOLVE_REF_NO_RECURSE,

Why are we passing RESOLVE_REF_NO_RECURSE here? I'd have thought we want to resolve the whole chain of symbolic refs to find out which ref is actually going to be updated.

> +						       NULL, &flags);
> +		if ((flags & REF_ISSYMREF) && resolved_ref) {

I think it is generally safer to check the return value before using any of the "out" parameters from a function call. In this case the function unconditionally clears flags at the beginning so it is safe.

> +			if (!starts_with(resolved_ref, "refs/heads/")) {
> +				decoration = decoration->next;
> +				continue;

This is the opposite of what I was expecting - if the decoration is a symref that resolves to a branch then that branch will also be in the list of decorations and so will be updated. If the decoration is a symref that resolves outside "refs/heads/" then we want to add the decoration to the list of refs to update to keep the current behavior.

If we do that then we skip all symbolic refs that point to another branch, instead of just skipping those that match HEAD and we don't need any of the changes below here.

Thanks
Phillip
Show 60 quoted lines
> +			}
> +
> +			strbuf_addstr(&update_ref, resolved_ref);
> +			ref = update_ref.buf;
> +			is_symref = 1;
> +		}
> +
> +		if ((is_symref && resolved_head_ref &&
> +		     !strcmp(resolved_head_ref, ref)) ||
> +		    string_list_has_string(&ctx->refs_to_oids, ref)) {
>   			decoration = decoration->next;
>   			continue;
>   		}
> @@ -6478,19 +6517,19 @@ static int add_decorations_to_list(const struct commit *commit,
>   		memset(item, 0, sizeof(*item));
>   
>   		/* If the branch is checked out, then leave a comment instead. */
> -		if ((path = branch_checked_out(decoration->name))) {
> +		if ((path = branch_checked_out(ref))) {
>   			item->command = TODO_COMMENT;
>   			strbuf_commented_addf(ctx->buf, comment_line_str,
>   					      "Ref %s checked out at '%s'\n",
> -					      decoration->name, path);
> +					      ref, path);
>   		} else {
>   			struct string_list_item *sti;
>   			item->command = TODO_UPDATE_REF;
> -			strbuf_addf(ctx->buf, "%s\n", decoration->name);
> +			strbuf_addf(ctx->buf, "%s\n", ref);
>   
>   			sti = string_list_insert(&ctx->refs_to_oids,
> -						 decoration->name);
> -			sti->util = init_update_ref_record(decoration->name);
> +						 ref);
> +			sti->util = init_update_ref_record(ref);
>   		}
>   
>   		item->offset_in_buf = base_offset;
> @@ -6501,6 +6540,8 @@ static int add_decorations_to_list(const struct commit *commit,
>   		decoration = decoration->next;
>   	}
>   
> +	strbuf_release(&update_ref);
> +	free(resolved_head_ref);
>   	return 0;
>   }
>   
> diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
> index 42ba8cc313..29447c0fc3 100755
> --- a/t/t3404-rebase-interactive.sh
> +++ b/t/t3404-rebase-interactive.sh
> @@ -1978,7 +1978,7 @@ test_expect_success '--update-refs ignores non-branch decorations' '
>   	test_cmp expect actual
>   '
>   
> -test_expect_failure '--update-refs skips branch symrefs to current branch' '
> +test_expect_success '--update-refs skips branch symrefs to current branch' '
>   	test_when_finished "
>   		test_might_fail git rebase --abort &&
>   		git checkout primary &&
Son Luong Ngoc via GitGitGadgetJun 3, 2026, 10:27 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH v2] rebase: skip branch symref aliases

From: Son Luong Ngoc <sluongng@gmail.com>

git rebase --update-refs can fail after the normal rebase path has updated the current branch when another local branch is a symref to it. This can happen during a default-branch rename where refs/heads/main points at refs/heads/master while users migrate.

The sequencer queues update-ref commands from local branch decorations. Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out decorations that are not local branches, such as HEAD and tags. A branch symref is different: it is still a local branch decoration, but if it resolves to another branch then that target branch is itself present in the decoration list and will be updated as a concrete branch.

Skip branch decorations whose symrefs resolve to refs/heads/*, because those targets are already represented by concrete branch decorations. This prevents aliases from scheduling a second update for the same branch. Keep symrefs to non-branch targets on the existing path.

Preserve the existing checked-out branch handling before applying these skips. Such refs still need a todo-list comment instead of an update-ref command, even when the checked-out ref is the branch being rebased or a branch symref alias. Use a copy of the resolved HEAD ref so later ref resolution does not overwrite it.

Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
---
    rebase: handle --update-refs branch symrefs
    
    Changes since v1:
    
     * Squashed the regression test and fix into a single patch.
     * Reworked the implementation per Phillip's review: skip local branch
       symrefs that are not checked out when their targets resolve to
       refs/heads/*, while preserving the existing behavior for symrefs to
       non-branch targets.
     * Preserved checked-out branch comments before applying the new symref
       skip, so worktrees still get the existing todo-list warning instead
       of an update-ref command.
     * Folded the symref coverage into the existing "--update-refs updates
       refs correctly" test, covering both an alias of HEAD and an alias of
       another branch.
     * Kept the relationship to 106b6885c7 in the commit message: non-local
       decorations are already filtered, and this patch handles the
       remaining local branch decorations that are symref aliases.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2126%2Fsluongng%2Fsl%2Frebase-update-refs-symrefs-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2126/sluongng/sl/rebase-update-refs-symrefs-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2126
Range-diff vs v1:
 1:  a550923440 < -:  ---------- t3404: add failing branch symref test
 2:  0ab0a71744 ! 1:  68f698225c rebase: skip branch symref aliases
     @@ Metadata
       ## Commit message ##
          rebase: skip branch symref aliases
      
     -    rebase --update-refs records local branch decorations before replaying
     -    commits. If a decoration is a symbolic branch such as refs/heads/main
     -    pointing at refs/heads/master, updating it later dereferences back to
     -    master and can fail because the normal rebase path already moved that
     -    branch.
     +    git rebase --update-refs can fail after the normal rebase path has
     +    updated the current branch when another local branch is a symref to it.
     +    This can happen during a default-branch rename where refs/heads/main
     +    points at refs/heads/master while users migrate.
      
     -    Resolve local branch symref decorations to their referents before
     -    queuing update-ref commands, and skip duplicates. This keeps branch
     -    aliases from scheduling a second update for the same underlying branch
     -    while still using the existing old-OID check for the single queued
     -    update.
     +    The sequencer queues update-ref commands from local branch decorations.
     +    Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out
     +    decorations that are not local branches, such as HEAD and tags. A branch
     +    symref is different: it is still a local branch decoration, but if it
     +    resolves to another branch then that target branch is itself present in
     +    the decoration list and will be updated as a concrete branch.
     +
     +    Skip branch decorations whose symrefs resolve to refs/heads/*, because
     +    those targets are already represented by concrete branch decorations.
     +    This prevents aliases from scheduling a second update for the same
     +    branch. Keep symrefs to non-branch targets on the existing path.
     +
     +    Preserve the existing checked-out branch handling before applying these
     +    skips. Such refs still need a todo-list comment instead of an update-ref
     +    command, even when the checked-out ref is the branch being rebased or a
     +    branch symref alias. Use a copy of the resolved HEAD ref so later ref
     +    resolution does not overwrite it.
      
          Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
      
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       	const struct name_decoration *decoration = get_name_decoration(&commit->object);
      -	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
      -						       "HEAD",
     -+	struct ref_store *refs = get_main_ref_store(the_repository);
     -+	const char *head_ref = refs_resolve_ref_unsafe(refs, "HEAD",
     - 						       RESOLVE_REF_READING,
     +-						       RESOLVE_REF_READING,
      -						       NULL,
      -						       NULL);
     -+						       NULL, NULL);
     -+	char *resolved_head_ref = refs_resolve_refdup(refs, "HEAD",
     -+						       RESOLVE_REF_READING,
     -+						       NULL, NULL);
     -+	struct strbuf update_ref = STRBUF_INIT;
     ++	struct ref_store *refs = get_main_ref_store(the_repository);
     ++	char *head_ref = refs_resolve_refdup(refs, "HEAD",
     ++					     RESOLVE_REF_READING,
     ++					     NULL, NULL);
       
       	while (decoration) {
       		struct todo_item *item;
       		const char *path;
     -+		const char *ref = decoration->name;
      +		const char *resolved_ref;
     -+		int is_symref = 0;
      +		int flags = 0;
       		size_t base_offset = ctx->buf->len;
       
       		/*
     -@@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
     - 		 * updated by the default rebase behavior.
     - 		 * Exclude it from the list of refs to update,
     - 		 * as well as any non-branch decorations.
     -+		 *
     -+		 * Resolve branch symrefs after checking for the current HEAD so
     -+		 * that aliases do not schedule duplicate updates for their
     -+		 * referents.
     -+		 *
     +-		 * If the branch is the current HEAD, then it will be
     +-		 * updated by the default rebase behavior.
     +-		 * Exclude it from the list of refs to update,
     +-		 * as well as any non-branch decorations.
       		 * Non-branch decorations may be present if the pretty format
       		 * includes "%d", which would have loaded all refs
       		 * into the global decoration table.
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
      +			continue;
      +		}
      +
     -+		if (head_ref && !strcmp(head_ref, ref)) {
     ++		path = branch_checked_out(decoration->name);
     ++
     ++		/*
     ++		 * If the branch is the current HEAD, then it will be
     ++		 * updated by the default rebase behavior. Exclude it from
     ++		 * the list of refs to update, unless it is checked out and
     ++		 * needs a comment in the todo list.
     ++		 */
     ++		if (!path && head_ref && !strcmp(head_ref, decoration->name)) {
      +			decoration = decoration->next;
      +			continue;
      +		}
      +
     -+		strbuf_reset(&update_ref);
     -+		resolved_ref = refs_resolve_ref_unsafe(refs, ref,
     -+						       RESOLVE_REF_READING |
     -+						       RESOLVE_REF_NO_RECURSE,
     ++		resolved_ref = refs_resolve_ref_unsafe(refs, decoration->name,
     ++						       RESOLVE_REF_READING,
      +						       NULL, &flags);
     -+		if ((flags & REF_ISSYMREF) && resolved_ref) {
     -+			if (!starts_with(resolved_ref, "refs/heads/")) {
     -+				decoration = decoration->next;
     -+				continue;
     -+			}
     -+
     -+			strbuf_addstr(&update_ref, resolved_ref);
     -+			ref = update_ref.buf;
     -+			is_symref = 1;
     -+		}
     -+
     -+		if ((is_symref && resolved_head_ref &&
     -+		     !strcmp(resolved_head_ref, ref)) ||
     -+		    string_list_has_string(&ctx->refs_to_oids, ref)) {
     ++		if (!path && resolved_ref && (flags & REF_ISSYMREF) &&
     ++		    starts_with(resolved_ref, "refs/heads/")) {
       			decoration = decoration->next;
       			continue;
       		}
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       
       		/* If the branch is checked out, then leave a comment instead. */
      -		if ((path = branch_checked_out(decoration->name))) {
     -+		if ((path = branch_checked_out(ref))) {
     ++		if (path) {
       			item->command = TODO_COMMENT;
       			strbuf_commented_addf(ctx->buf, comment_line_str,
       					      "Ref %s checked out at '%s'\n",
     --					      decoration->name, path);
     -+					      ref, path);
     - 		} else {
     - 			struct string_list_item *sti;
     - 			item->command = TODO_UPDATE_REF;
     --			strbuf_addf(ctx->buf, "%s\n", decoration->name);
     -+			strbuf_addf(ctx->buf, "%s\n", ref);
     - 
     - 			sti = string_list_insert(&ctx->refs_to_oids,
     --						 decoration->name);
     --			sti->util = init_update_ref_record(decoration->name);
     -+						 ref);
     -+			sti->util = init_update_ref_record(ref);
     - 		}
     - 
     - 		item->offset_in_buf = base_offset;
      @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       		decoration = decoration->next;
       	}
       
     -+	strbuf_release(&update_ref);
     -+	free(resolved_head_ref);
     ++	free(head_ref);
       	return 0;
       }
       
      
       ## t/t3404-rebase-interactive.sh ##
      @@ t/t3404-rebase-interactive.sh: test_expect_success '--update-refs ignores non-branch decorations' '
     - 	test_cmp expect actual
       '
       
     --test_expect_failure '--update-refs skips branch symrefs to current branch' '
     -+test_expect_success '--update-refs skips branch symrefs to current branch' '
     - 	test_when_finished "
     - 		test_might_fail git rebase --abort &&
     - 		git checkout primary &&
     + test_expect_success '--update-refs updates refs correctly' '
     ++	test_when_finished "
     ++		test_might_fail git symbolic-ref -d refs/heads/no-conflict-branch-alias &&
     ++		test_might_fail git symbolic-ref -d refs/heads/second-alias
     ++	" &&
     + 	git checkout -B update-refs no-conflict-branch &&
     + 	git branch -f base HEAD~4 &&
     + 	git branch -f first HEAD~3 &&
     + 	git branch -f second HEAD~3 &&
     + 	git branch -f third HEAD~1 &&
     ++	git symbolic-ref refs/heads/no-conflict-branch-alias \
     ++		refs/heads/no-conflict-branch &&
     ++	git symbolic-ref refs/heads/second-alias refs/heads/second &&
     + 	test_commit extra2 fileX &&
     + 	git commit --amend --fixup=L &&
     + 
     +@@ t/t3404-rebase-interactive.sh: test_expect_success '--update-refs updates refs correctly' '
     + 
     + 	test_cmp_rev HEAD~3 refs/heads/first &&
     + 	test_cmp_rev HEAD~3 refs/heads/second &&
     ++	test_cmp_rev HEAD~3 refs/heads/second-alias &&
     + 	test_cmp_rev HEAD~1 refs/heads/third &&
     + 	test_cmp_rev HEAD refs/heads/no-conflict-branch &&
     ++	test_cmp_rev HEAD refs/heads/no-conflict-branch-alias &&
     ++	test_write_lines refs/heads/no-conflict-branch >expect &&
     ++	git symbolic-ref refs/heads/no-conflict-branch-alias >actual &&
     ++	test_cmp expect actual &&
     ++	test_write_lines refs/heads/second >expect &&
     ++	git symbolic-ref refs/heads/second-alias >actual &&
     ++	test_cmp expect actual &&
     + 
     + 	q_to_tab >expect <<-\EOF &&
     + 	Successfully rebased and updated refs/heads/update-refs.
 sequencer.c                   | 43 +++++++++++++++++++++++++----------
 t/t3404-rebase-interactive.sh | 15 ++++++++++++
 2 files changed, 46 insertions(+), 12 deletions(-)
Show changes to 2 files +46 −12

sequencer.c, t/t3404-rebase-interactive.sh

diff --git a/sequencer.c b/sequencer.c
index 1ee4b2875b..6ab8b47108 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6445,28 +6445,46 @@ static int add_decorations_to_list(const struct commit *commit,
 				   struct todo_add_branch_context *ctx)
 {
 	const struct name_decoration *decoration = get_name_decoration(&commit->object);
-	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
-						       "HEAD",
-						       RESOLVE_REF_READING,
-						       NULL,
-						       NULL);
+	struct ref_store *refs = get_main_ref_store(the_repository);
+	char *head_ref = refs_resolve_refdup(refs, "HEAD",
+					     RESOLVE_REF_READING,
+					     NULL, NULL);
 
 	while (decoration) {
 		struct todo_item *item;
 		const char *path;
+		const char *resolved_ref;
+		int flags = 0;
 		size_t base_offset = ctx->buf->len;
 
 		/*
-		 * If the branch is the current HEAD, then it will be
-		 * updated by the default rebase behavior.
-		 * Exclude it from the list of refs to update,
-		 * as well as any non-branch decorations.
 		 * Non-branch decorations may be present if the pretty format
 		 * includes "%d", which would have loaded all refs
 		 * into the global decoration table.
 		 */
-		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
-		    (decoration->type != DECORATION_REF_LOCAL)) {
+		if (decoration->type != DECORATION_REF_LOCAL) {
+			decoration = decoration->next;
+			continue;
+		}
+
+		path = branch_checked_out(decoration->name);
+
+		/*
+		 * If the branch is the current HEAD, then it will be
+		 * updated by the default rebase behavior. Exclude it from
+		 * the list of refs to update, unless it is checked out and
+		 * needs a comment in the todo list.
+		 */
+		if (!path && head_ref && !strcmp(head_ref, decoration->name)) {
+			decoration = decoration->next;
+			continue;
+		}
+
+		resolved_ref = refs_resolve_ref_unsafe(refs, decoration->name,
+						       RESOLVE_REF_READING,
+						       NULL, &flags);
+		if (!path && resolved_ref && (flags & REF_ISSYMREF) &&
+		    starts_with(resolved_ref, "refs/heads/")) {
 			decoration = decoration->next;
 			continue;
 		}
@@ -6478,7 +6496,7 @@ static int add_decorations_to_list(const struct commit *commit,
 		memset(item, 0, sizeof(*item));
 
 		/* If the branch is checked out, then leave a comment instead. */
-		if ((path = branch_checked_out(decoration->name))) {
+		if (path) {
 			item->command = TODO_COMMENT;
 			strbuf_commented_addf(ctx->buf, comment_line_str,
 					      "Ref %s checked out at '%s'\n",
@@ -6501,6 +6519,7 @@ static int add_decorations_to_list(const struct commit *commit,
 		decoration = decoration->next;
 	}
 
+	free(head_ref);
 	return 0;
 }
 
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 58b3bb0c27..bc0b6a31f7 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1979,11 +1979,18 @@ test_expect_success '--update-refs ignores non-branch decorations' '
 '
 
 test_expect_success '--update-refs updates refs correctly' '
+	test_when_finished "
+		test_might_fail git symbolic-ref -d refs/heads/no-conflict-branch-alias &&
+		test_might_fail git symbolic-ref -d refs/heads/second-alias
+	" &&
 	git checkout -B update-refs no-conflict-branch &&
 	git branch -f base HEAD~4 &&
 	git branch -f first HEAD~3 &&
 	git branch -f second HEAD~3 &&
 	git branch -f third HEAD~1 &&
+	git symbolic-ref refs/heads/no-conflict-branch-alias \
+		refs/heads/no-conflict-branch &&
+	git symbolic-ref refs/heads/second-alias refs/heads/second &&
 	test_commit extra2 fileX &&
 	git commit --amend --fixup=L &&
 
@@ -1991,8 +1998,16 @@ test_expect_success '--update-refs updates refs correctly' '
 
 	test_cmp_rev HEAD~3 refs/heads/first &&
 	test_cmp_rev HEAD~3 refs/heads/second &&
+	test_cmp_rev HEAD~3 refs/heads/second-alias &&
 	test_cmp_rev HEAD~1 refs/heads/third &&
 	test_cmp_rev HEAD refs/heads/no-conflict-branch &&
+	test_cmp_rev HEAD refs/heads/no-conflict-branch-alias &&
+	test_write_lines refs/heads/no-conflict-branch >expect &&
+	git symbolic-ref refs/heads/no-conflict-branch-alias >actual &&
+	test_cmp expect actual &&
+	test_write_lines refs/heads/second >expect &&
+	git symbolic-ref refs/heads/second-alias >actual &&
+	test_cmp expect actual &&
 
 	q_to_tab >expect <<-\EOF &&
 	Successfully rebased and updated refs/heads/update-refs.

base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d
-- 
gitgitgadget
Phillip WoodJun 4, 2026, 15:37 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH v2] rebase: skip branch symref aliases

On 03/06/2026 11:27, Son Luong Ngoc via GitGitGadget wrote:
Show 18 quoted lines
> From: Son Luong Ngoc <sluongng@gmail.com>
> 
> git rebase --update-refs can fail after the normal rebase path has
> updated the current branch when another local branch is a symref to it.
> This can happen during a default-branch rename where refs/heads/main
> points at refs/heads/master while users migrate.
> 
> The sequencer queues update-ref commands from local branch decorations.
> Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out
> decorations that are not local branches, such as HEAD and tags. A branch
> symref is different: it is still a local branch decoration, but if it
> resolves to another branch then that target branch is itself present in
> the decoration list and will be updated as a concrete branch.
> 
> Skip branch decorations whose symrefs resolve to refs/heads/*, because
> those targets are already represented by concrete branch decorations.
> This prevents aliases from scheduling a second update for the same
> branch. Keep symrefs to non-branch targets on the existing path.
Makes sense
Show 5 quoted lines
> Preserve the existing checked-out branch handling before applying these
> skips. Such refs still need a todo-list comment instead of an update-ref
> command, even when the checked-out ref is the branch being rebased or a
> branch symref alias. Use a copy of the resolved HEAD ref so later ref
> resolution does not overwrite it.

I don't quite understand this. A symref that points to another branch should always be skipped. When we look up which branches are checked out (see worktree.c:add_head_info()) we use

  	refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
				 "HEAD",
				 0,
				 &wt->head_oid, &flags);

so it will never report a symref as being checked out - it always resolves any symrefs first.

If we have a symref pointing somewhere outside of "refs/heads" then we need to check whether the target is checked out, not the symref itself. I'm not sure how likely that is to happen in practice.

Show 17 quoted lines
> diff --git a/sequencer.c b/sequencer.c
> index 1ee4b2875b..6ab8b47108 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -6445,28 +6445,46 @@ static int add_decorations_to_list(const struct commit *commit,
>   				   struct todo_add_branch_context *ctx)
>   {
>   	const struct name_decoration *decoration = get_name_decoration(&commit->object);
> -	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
> -						       "HEAD",
> -						       RESOLVE_REF_READING,
> -						       NULL,
> -						       NULL);
> +	struct ref_store *refs = get_main_ref_store(the_repository);
> +	char *head_ref = refs_resolve_refdup(refs, "HEAD",
> +					     RESOLVE_REF_READING,
> +					     NULL, NULL);
This part and the test look good now
Show 22 quoted lines
>   	while (decoration) {
>   		struct todo_item *item;
>   		const char *path;
> +		const char *resolved_ref;
> +		int flags = 0;
>   		size_t base_offset = ctx->buf->len;
>   
>   		/*
> -		 * If the branch is the current HEAD, then it will be
> -		 * updated by the default rebase behavior.
> -		 * Exclude it from the list of refs to update,
> -		 * as well as any non-branch decorations.
>   		 * Non-branch decorations may be present if the pretty format
>   		 * includes "%d", which would have loaded all refs
>   		 * into the global decoration table.
>   		 */
> -		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
> -		    (decoration->type != DECORATION_REF_LOCAL)) {
> +		if (decoration->type != DECORATION_REF_LOCAL) {
> +			decoration = decoration->next;
> +			continue;
> +		}

If a decoration matches the current branch why don't we just skip it like we used to? (As an aside the existing code in wrong because if the user runs "git rebase --update-refs <upstream> <branch>" HEAD does not point to "<branch>" but lets not worry about that now)

> +		path = branch_checked_out(decoration->name);

As I said above if the symref target is anther branch we should skip it and if the target is not a branch then we need to check if the target is checked out so we need to resolve the ref before calling branch_checked_out().

Thanks
Phillip
Son Luong Ngoc via GitGitGadgetJul 22, 2026, 08:15 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH v3 0/2] rebase: handle --update-refs branch symrefs

git rebase --update-refs can finish rewriting the current branch and then fail while updating a local branch that is a symbolic ref. This can happen during a default-branch rename where refs/heads/main points at refs/heads/master while users migrate. The failure leaves refs partially updated even though the main rebase has succeeded.

Resolve local branch decorations before adding update-ref commands. The first patch skips aliases whose targets are other branches and preserves the existing handling of the current branch. The second patch keeps aliases to non-branch refs supported while preventing duplicate and cross-worktree updates to their resolved targets.

Changes since v2:
 * Skip branch-to-branch symrefs before checked-out handling.
 * Restore the unconditional current-branch skip and keep an owned copy of
   the resolved HEAD name.
 * Check both a non-branch symref alias and its resolved target against
   checked-out reservations.
 * Deduplicate aliases that share a non-branch target.
 * Reserve resolved targets from other worktrees' in-progress update-refs
   state.
 * Split the branch-alias fix and non-branch safeguards into separate
   patches.
 * Rebase onto 48bbf81c29 (The 5th batch).

The focused t3400 and t3404 test suites pass with both the files and reftable backends.

Son Luong Ngoc (2):
  rebase: skip branch symref aliases
  rebase: guard non-branch symref targets
 branch.c                      | 15 ++++++
 sequencer.c                   | 63 ++++++++++++++++++++-----
 t/t3400-rebase.sh             |  2 +-
 t/t3404-rebase-interactive.sh | 88 +++++++++++++++++++++++++++++++++++
 4 files changed, 155 insertions(+), 13 deletions(-)
base-commit: 48bbf81c29ca9a4479ec7850fe206518682cdb2f
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2126%2Fsluongng%2Fsl%2Frebase-update-refs-symrefs-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2126/sluongng/sl/rebase-update-refs-symrefs-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2126
Range-diff vs v2:
 1:  68f698225c ! 1:  b9a01e9141 rebase: skip branch symref aliases
     @@ Metadata
       ## Commit message ##
          rebase: skip branch symref aliases
      
     -    git rebase --update-refs can fail after the normal rebase path has
     -    updated the current branch when another local branch is a symref to it.
     -    This can happen during a default-branch rename where refs/heads/main
     -    points at refs/heads/master while users migrate.
     +    git rebase --update-refs can finish rewriting the current branch and
     +    then fail while updating a local branch that is a symbolic ref. This can
     +    happen during a default-branch rename where refs/heads/main points at
     +    refs/heads/master while users migrate.
      
     -    The sequencer queues update-ref commands from local branch decorations.
     -    Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out
     -    decorations that are not local branches, such as HEAD and tags. A branch
     -    symref is different: it is still a local branch decoration, but if it
     -    resolves to another branch then that target branch is itself present in
     -    the decoration list and will be updated as a concrete branch.
     +    The problem is a partially applied ref update: the main rebase has
     +    already succeeded when the later ref update fails.
      
     -    Skip branch decorations whose symrefs resolve to refs/heads/*, because
     -    those targets are already represented by concrete branch decorations.
     -    This prevents aliases from scheduling a second update for the same
     -    branch. Keep symrefs to non-branch targets on the existing path.
     +    The sequencer queues updates from local branch decorations. Commit
     +    106b6885c7 (rebase: ignore non-branch update-refs) filters out
     +    decorations such as HEAD and tags. A branch symref is still a local
     +    branch decoration, but refs_update_ref() dereferences it, so an alias to
     +    another branch duplicates the concrete branch update.
      
     -    Preserve the existing checked-out branch handling before applying these
     -    skips. Such refs still need a todo-list comment instead of an update-ref
     -    command, even when the checked-out ref is the branch being rebased or a
     -    branch symref alias. Use a copy of the resolved HEAD ref so later ref
     -    resolution does not overwrite it.
     +    Resolve local branch decorations before queuing them. Skip symrefs whose
     +    targets are under refs/heads/ so that only the concrete branch update is
     +    queued. Keep an owned copy of the resolved HEAD and skip the current
     +    branch before checked-out handling so later ref resolution cannot change
     +    the comparison.
     +
     +    This prevents a successful rebase from being followed by a failed,
     +    partially applied ref update while preserving each alias as a symref.
      
          Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
      
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       	while (decoration) {
       		struct todo_item *item;
       		const char *path;
     -+		const char *resolved_ref;
     ++		char *resolved_ref;
      +		int flags = 0;
       		size_t base_offset = ctx->buf->len;
       
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
      +			continue;
      +		}
      +
     -+		path = branch_checked_out(decoration->name);
     -+
     -+		/*
     -+		 * If the branch is the current HEAD, then it will be
     -+		 * updated by the default rebase behavior. Exclude it from
     -+		 * the list of refs to update, unless it is checked out and
     -+		 * needs a comment in the todo list.
     -+		 */
     -+		if (!path && head_ref && !strcmp(head_ref, decoration->name)) {
     ++		resolved_ref = refs_resolve_refdup(refs, decoration->name,
     ++						      RESOLVE_REF_READING,
     ++						      NULL, &flags);
     ++		if (resolved_ref && (flags & REF_ISSYMREF) &&
     ++		    starts_with(resolved_ref, "refs/heads/")) {
     ++			free(resolved_ref);
      +			decoration = decoration->next;
      +			continue;
      +		}
      +
     -+		resolved_ref = refs_resolve_ref_unsafe(refs, decoration->name,
     -+						       RESOLVE_REF_READING,
     -+						       NULL, &flags);
     -+		if (!path && resolved_ref && (flags & REF_ISSYMREF) &&
     -+		    starts_with(resolved_ref, "refs/heads/")) {
     ++		/*
     ++		 * If the branch is the current HEAD, then it will be
     ++		 * updated by the default rebase behavior.
     ++		 */
     ++		if (head_ref && !strcmp(head_ref, decoration->name)) {
     ++			free(resolved_ref);
       			decoration = decoration->next;
       			continue;
       		}
     + 
     ++		path = branch_checked_out(decoration->name);
     ++
     + 		ALLOC_GROW(ctx->items,
     + 			ctx->items_nr + 1,
     + 			ctx->items_alloc);
      @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       		memset(item, 0, sizeof(*item));
       
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       			strbuf_commented_addf(ctx->buf, comment_line_str,
       					      "Ref %s checked out at '%s'\n",
      @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
     + 		item->arg_len = ctx->buf->len - base_offset;
     + 		ctx->items_nr++;
     + 
     ++		free(resolved_ref);
       		decoration = decoration->next;
       	}
       
     @@ sequencer.c: static int add_decorations_to_list(const struct commit *commit,
       }
       
      
     + ## t/t3400-rebase.sh ##
     +@@ t/t3400-rebase.sh: test_expect_success 'git rebase --update-ref with core.commentChar and branch on
     + 	GIT_SEQUENCE_EDITOR="cat >actual" git -c core.commentChar=% \
     + 		 rebase -i --update-refs base &&
     + 	test_grep "% Ref refs/heads/wt-topic checked out at" actual &&
     +-	test_grep "% Ref refs/heads/topic2 checked out at" actual
     ++	test_grep ! "% Ref refs/heads/topic2 checked out at" actual
     + '
     + 
     + test_done
     +
       ## t/t3404-rebase-interactive.sh ##
      @@ t/t3404-rebase-interactive.sh: test_expect_success '--update-refs ignores non-branch decorations' '
     + 	) &&
     + 	grep ^update-ref todo >actual &&
     + 	test_write_lines "update-ref refs/heads/no-conflict-branch" >expect &&
     ++	test_grep ! "^# Ref refs/heads/update-refs checked out" todo &&
     + 	test_cmp expect actual
       '
       
       test_expect_success '--update-refs updates refs correctly' '
 -:  ---------- > 2:  a653f56ea2 rebase: guard non-branch symref targets
-- 
gitgitgadget
Son Luong Ngoc via GitGitGadgetJul 22, 2026, 08:15 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH v3 1/2] rebase: skip branch symref aliases

From: Son Luong Ngoc <sluongng@gmail.com>

git rebase --update-refs can finish rewriting the current branch and then fail while updating a local branch that is a symbolic ref. This can happen during a default-branch rename where refs/heads/main points at refs/heads/master while users migrate.

The problem is a partially applied ref update: the main rebase has already succeeded when the later ref update fails.

The sequencer queues updates from local branch decorations. Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out decorations such as HEAD and tags. A branch symref is still a local branch decoration, but refs_update_ref() dereferences it, so an alias to another branch duplicates the concrete branch update.

Resolve local branch decorations before queuing them. Skip symrefs whose targets are under refs/heads/ so that only the concrete branch update is queued. Keep an owned copy of the resolved HEAD and skip the current branch before checked-out handling so later ref resolution cannot change the comparison.

This prevents a successful rebase from being followed by a failed, partially applied ref update while preserving each alias as a symref.

Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
---
 sequencer.c                   | 44 +++++++++++++++++++++++++----------
 t/t3400-rebase.sh             |  2 +-
 t/t3404-rebase-interactive.sh | 16 +++++++++++++
 3 files changed, 49 insertions(+), 13 deletions(-)
Show changes to 3 files +49 −13

sequencer.c, t/t3400-rebase.sh, t/t3404-rebase-interactive.sh

diff --git a/sequencer.c b/sequencer.c
index 1355a99a09..63aba60a08 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6465,32 +6465,50 @@ static int add_decorations_to_list(const struct commit *commit,
 				   struct todo_add_branch_context *ctx)
 {
 	const struct name_decoration *decoration = get_name_decoration(&commit->object);
-	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
-						       "HEAD",
-						       RESOLVE_REF_READING,
-						       NULL,
-						       NULL);
+	struct ref_store *refs = get_main_ref_store(the_repository);
+	char *head_ref = refs_resolve_refdup(refs, "HEAD",
+					     RESOLVE_REF_READING,
+					     NULL, NULL);
 
 	while (decoration) {
 		struct todo_item *item;
 		const char *path;
+		char *resolved_ref;
+		int flags = 0;
 		size_t base_offset = ctx->buf->len;
 
 		/*
-		 * If the branch is the current HEAD, then it will be
-		 * updated by the default rebase behavior.
-		 * Exclude it from the list of refs to update,
-		 * as well as any non-branch decorations.
 		 * Non-branch decorations may be present if the pretty format
 		 * includes "%d", which would have loaded all refs
 		 * into the global decoration table.
 		 */
-		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
-		    (decoration->type != DECORATION_REF_LOCAL)) {
+		if (decoration->type != DECORATION_REF_LOCAL) {
+			decoration = decoration->next;
+			continue;
+		}
+
+		resolved_ref = refs_resolve_refdup(refs, decoration->name,
+						      RESOLVE_REF_READING,
+						      NULL, &flags);
+		if (resolved_ref && (flags & REF_ISSYMREF) &&
+		    starts_with(resolved_ref, "refs/heads/")) {
+			free(resolved_ref);
+			decoration = decoration->next;
+			continue;
+		}
+
+		/*
+		 * If the branch is the current HEAD, then it will be
+		 * updated by the default rebase behavior.
+		 */
+		if (head_ref && !strcmp(head_ref, decoration->name)) {
+			free(resolved_ref);
 			decoration = decoration->next;
 			continue;
 		}
 
+		path = branch_checked_out(decoration->name);
+
 		ALLOC_GROW(ctx->items,
 			ctx->items_nr + 1,
 			ctx->items_alloc);
@@ -6498,7 +6516,7 @@ static int add_decorations_to_list(const struct commit *commit,
 		memset(item, 0, sizeof(*item));
 
 		/* If the branch is checked out, then leave a comment instead. */
-		if ((path = branch_checked_out(decoration->name))) {
+		if (path) {
 			item->command = TODO_COMMENT;
 			strbuf_commented_addf(ctx->buf, comment_line_str,
 					      "Ref %s checked out at '%s'\n",
@@ -6518,9 +6536,11 @@ static int add_decorations_to_list(const struct commit *commit,
 		item->arg_len = ctx->buf->len - base_offset;
 		ctx->items_nr++;
 
+		free(resolved_ref);
 		decoration = decoration->next;
 	}
 
+	free(head_ref);
 	return 0;
 }
 
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index e62e07b894..1a02f6546b 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -471,7 +471,7 @@ test_expect_success 'git rebase --update-ref with core.commentChar and branch on
 	GIT_SEQUENCE_EDITOR="cat >actual" git -c core.commentChar=% \
 		 rebase -i --update-refs base &&
 	test_grep "% Ref refs/heads/wt-topic checked out at" actual &&
-	test_grep "% Ref refs/heads/topic2 checked out at" actual
+	test_grep ! "% Ref refs/heads/topic2 checked out at" actual
 '
 
 test_done
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index e64816770a..11afa8be56 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -1975,15 +1975,23 @@ test_expect_success '--update-refs ignores non-branch decorations' '
 	) &&
 	grep ^update-ref todo >actual &&
 	test_write_lines "update-ref refs/heads/no-conflict-branch" >expect &&
+	test_grep ! "^# Ref refs/heads/update-refs checked out" todo &&
 	test_cmp expect actual
 '
 
 test_expect_success '--update-refs updates refs correctly' '
+	test_when_finished "
+		test_might_fail git symbolic-ref -d refs/heads/no-conflict-branch-alias &&
+		test_might_fail git symbolic-ref -d refs/heads/second-alias
+	" &&
 	git checkout -B update-refs no-conflict-branch &&
 	git branch -f base HEAD~4 &&
 	git branch -f first HEAD~3 &&
 	git branch -f second HEAD~3 &&
 	git branch -f third HEAD~1 &&
+	git symbolic-ref refs/heads/no-conflict-branch-alias \
+		refs/heads/no-conflict-branch &&
+	git symbolic-ref refs/heads/second-alias refs/heads/second &&
 	test_commit extra2 fileX &&
 	git commit --amend --fixup=L &&
 
@@ -1991,8 +1999,16 @@ test_expect_success '--update-refs updates refs correctly' '
 
 	test_cmp_rev HEAD~3 refs/heads/first &&
 	test_cmp_rev HEAD~3 refs/heads/second &&
+	test_cmp_rev HEAD~3 refs/heads/second-alias &&
 	test_cmp_rev HEAD~1 refs/heads/third &&
 	test_cmp_rev HEAD refs/heads/no-conflict-branch &&
+	test_cmp_rev HEAD refs/heads/no-conflict-branch-alias &&
+	test_write_lines refs/heads/no-conflict-branch >expect &&
+	git symbolic-ref refs/heads/no-conflict-branch-alias >actual &&
+	test_cmp expect actual &&
+	test_write_lines refs/heads/second >expect &&
+	git symbolic-ref refs/heads/second-alias >actual &&
+	test_cmp expect actual &&
 
 	q_to_tab >expect <<-\EOF &&
 	Successfully rebased and updated refs/heads/update-refs.
-- 
gitgitgadget
Son Luong Ngoc via GitGitGadgetJul 22, 2026, 08:15 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

[PATCH v3 2/2] rebase: guard non-branch symref targets

From: Son Luong Ngoc <sluongng@gmail.com>

A local branch symbolic ref may point outside refs/heads/. Such an alias cannot be skipped like a branch-to-branch alias because its concrete target ref is absent from the local branch decoration list.

However, queuing each alias independently can update the same target ref more than once and make the second compare-and-swap fail. A reservation from another worktree can also name either an alias or its resolved target ref, so checking only one form can miss an in-progress update.

Fix these cases by checking both the literal alias and its resolved target ref against checked-out reservations. Deduplicate updates by target ref. Also reserve both forms when loading another worktree's update-refs state. This makes different aliases honor the same in-progress update.

This keeps non-branch symrefs supported without allowing duplicate or cross-worktree ref updates.

Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
---
 branch.c                      | 15 ++++++++
 sequencer.c                   | 19 +++++++++
 t/t3404-rebase-interactive.sh | 72 +++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
Show changes to 3 files +106 −0

branch.c, sequencer.c, t/t3404-rebase-interactive.sh

diff --git a/branch.c b/branch.c
index 243db7d0fc..98a50d8368 100644
--- a/branch.c
+++ b/branch.c
@@ -442,10 +442,25 @@ static void prepare_checked_out_branches(void)
 						     &update_refs)) {
 			struct string_list_item *item;
 			for_each_string_list_item(item, &update_refs) {
+				char *resolved_ref;
+				int flags = 0;
+
 				old = strmap_put(&current_checked_out_branches,
 						 item->string,
 						 xstrdup(wt->path));
 				free(old);
+
+				resolved_ref = refs_resolve_refdup(
+					get_main_ref_store(the_repository),
+					item->string, RESOLVE_REF_READING,
+					NULL, &flags);
+				if (resolved_ref && (flags & REF_ISSYMREF)) {
+					old = strmap_put(
+						&current_checked_out_branches,
+						resolved_ref, xstrdup(wt->path));
+					free(old);
+				}
+				free(resolved_ref);
 			}
 			string_list_clear(&update_refs, 1);
 		}
diff --git a/sequencer.c b/sequencer.c
index 63aba60a08..040b5bf645 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6459,6 +6459,7 @@ struct todo_add_branch_context {
 	size_t items_alloc;
 	struct strbuf *buf;
 	struct string_list refs_to_oids;
+	struct string_list symref_update_targets;
 };
 
 static int add_decorations_to_list(const struct commit *commit,
@@ -6473,6 +6474,7 @@ static int add_decorations_to_list(const struct commit *commit,
 	while (decoration) {
 		struct todo_item *item;
 		const char *path;
+		const char *checked_ref;
 		char *resolved_ref;
 		int flags = 0;
 		size_t base_offset = ctx->buf->len;
@@ -6508,6 +6510,17 @@ static int add_decorations_to_list(const struct commit *commit,
 		}
 
 		path = branch_checked_out(decoration->name);
+		if (!path && resolved_ref && (flags & REF_ISSYMREF)) {
+			checked_ref = resolved_ref;
+			path = branch_checked_out(checked_ref);
+		}
+		if (!path && resolved_ref && (flags & REF_ISSYMREF) &&
+		    string_list_has_string(&ctx->symref_update_targets,
+					   resolved_ref)) {
+			free(resolved_ref);
+			decoration = decoration->next;
+			continue;
+		}
 
 		ALLOC_GROW(ctx->items,
 			ctx->items_nr + 1,
@@ -6523,6 +6536,10 @@ static int add_decorations_to_list(const struct commit *commit,
 					      decoration->name, path);
 		} else {
 			struct string_list_item *sti;
+
+			if (resolved_ref && (flags & REF_ISSYMREF))
+				string_list_insert(&ctx->symref_update_targets,
+						   resolved_ref);
 			item->command = TODO_UPDATE_REF;
 			strbuf_addf(ctx->buf, "%s\n", decoration->name);
 
@@ -6554,6 +6571,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
 	struct todo_add_branch_context ctx = {
 		.buf = &todo_list->buf,
 		.refs_to_oids = STRING_LIST_INIT_DUP,
+		.symref_update_targets = STRING_LIST_INIT_DUP,
 	};
 
 	ctx.items_alloc = 2 * todo_list->nr + 1;
@@ -6579,6 +6597,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
 	res = write_update_refs_state(&ctx.refs_to_oids);
 
 	string_list_clear(&ctx.refs_to_oids, 1);
+	string_list_clear(&ctx.symref_update_targets, 0);
 
 	if (res) {
 		/* we failed, so clean up the new list. */
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 11afa8be56..110ed8ae63 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -2024,6 +2024,78 @@ test_expect_success '--update-refs updates refs correctly' '
 	test_cmp expect err.trimmed
 '
 
+test_expect_success '--update-refs checks resolved non-branch symref target' '
+	test_when_finished "
+		git worktree remove --force checked-out-target-wt &&
+		git symbolic-ref -d refs/heads/non-branch-alias &&
+		git tag -d checked-out-target
+	" &&
+	git tag checked-out-target HEAD~1 &&
+	git symbolic-ref refs/heads/non-branch-alias refs/tags/checked-out-target &&
+	git worktree add --detach checked-out-target-wt checked-out-target &&
+	git -C checked-out-target-wt symbolic-ref HEAD refs/tags/checked-out-target &&
+
+	GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 &&
+
+	test_grep "^# Ref refs/heads/non-branch-alias checked out at" todo &&
+	test_write_lines refs/tags/checked-out-target >expect &&
+	git symbolic-ref refs/heads/non-branch-alias >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--update-refs deduplicates non-branch symref targets' '
+	test_when_finished "
+		git symbolic-ref -d refs/heads/non-branch-alias-one &&
+		git symbolic-ref -d refs/heads/non-branch-alias-two &&
+		git tag -d shared-non-branch-target
+	" &&
+	git tag shared-non-branch-target HEAD~1 &&
+	git symbolic-ref refs/heads/non-branch-alias-one \
+		refs/tags/shared-non-branch-target &&
+	git symbolic-ref refs/heads/non-branch-alias-two \
+		refs/tags/shared-non-branch-target &&
+
+	GIT_SEQUENCE_EDITOR=: git rebase -i --force-rebase --update-refs HEAD~2 &&
+
+	test_cmp_rev HEAD~1 refs/heads/non-branch-alias-one &&
+	test_cmp_rev HEAD~1 refs/heads/non-branch-alias-two &&
+	test_write_lines refs/tags/shared-non-branch-target >expect &&
+	git symbolic-ref refs/heads/non-branch-alias-one >actual &&
+	test_cmp expect actual &&
+	git symbolic-ref refs/heads/non-branch-alias-two >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--update-refs honors non-branch symref reservations' '
+	test_when_finished "
+		test_might_fail git worktree remove --force reserved-target-wt &&
+		test_might_fail git symbolic-ref -d \
+			refs/heads/reserved-non-branch-alias-one &&
+		test_might_fail git symbolic-ref -d \
+			refs/heads/reserved-non-branch-alias-two &&
+		test_might_fail git tag -d reserved-non-branch-target
+	" &&
+	git tag reserved-non-branch-target HEAD~1 &&
+	git symbolic-ref refs/heads/reserved-non-branch-alias-one \
+		refs/tags/reserved-non-branch-target &&
+	git symbolic-ref refs/heads/reserved-non-branch-alias-two \
+		refs/tags/reserved-non-branch-target &&
+	git worktree add --detach reserved-target-wt HEAD &&
+	wt_gitdir=$(git -C reserved-target-wt rev-parse --absolute-git-dir) &&
+	mkdir -p "$wt_gitdir/rebase-merge" &&
+	old_oid=$(git rev-parse refs/heads/reserved-non-branch-alias-one) &&
+	test_write_lines refs/heads/reserved-non-branch-alias-one \
+		"$old_oid" "$old_oid" >"$wt_gitdir/rebase-merge/update-refs" &&
+
+	GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 &&
+
+	test_grep "^# Ref refs/heads/reserved-non-branch-alias-one checked out at" \
+		todo &&
+	test_grep "^# Ref refs/heads/reserved-non-branch-alias-two checked out at" \
+		todo &&
+	test_grep ! "^update-ref refs/heads/reserved-non-branch-alias" todo
+'
+
 test_expect_success 'respect user edits to update-ref steps' '
 	git checkout -B update-refs-break no-conflict-branch &&
 	git branch -f base HEAD~4 &&
-- 
gitgitgadget
Son Luong NgocJul 22, 2026, 08:16 UTC in reply to Phillip Wood on lore

Re: [PATCH v2] rebase: skip branch symref aliases

On 04/06/2026 16:46, Phillip Wood wrote:
Show 11 quoted lines
> A symref that points to another branch should always be skipped. When we
> look up which branches are checked out (see worktree.c:add_head_info()) we
> use
>
> 	refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
> 				 "HEAD",
> 				 0,
> 				 &wt->head_oid, &flags);
>
> so it will never report a symref as being checked out - it always resolves
> any symrefs first.

Yes, this is the right distinction. Patch 1 now resolves each local branch decoration before deciding whether to queue it. If the target is under refs/heads/, the alias is skipped unconditionally and the concrete branch decoration remains the only update that is queued.

> If we have a symref pointing somewhere outside of "refs/heads" then we
> need to check whether the target is checked out, not the symref itself.
> I'm not sure how likely that is to happen in practice.

Patch 2 handles that case separately. It checks both the literal alias and its resolved target ref against the checked-out reservations. I also added a test with a non-branch target checked out in another worktree.

> If a decoration matches the current branch why don't we just skip it like
> we used to? (As an aside the existing code in wrong because if the user
> runs "git rebase --update-refs <upstream> <branch>" HEAD does not point to
> "<branch>" but lets not worry about that now)

Agreed. Patch 1 now skips the current branch before checked-out handling, as the old code did.

The contrary expectation in t3400 came from head_ref pointing into a buffer that was reused while resolving another decoration. That could make the current-branch comparison fail. head_ref is now an owned copy. The test expects the current branch to be omitted from the todo list.

While adding the non-branch coverage, I found that two aliases to the same target ref could queue the same update twice and make the second compare-and-swap fail. Patch 2 deduplicates those updates by target ref. It also records resolved target refs from other worktrees' in-progress update-refs state so that a different alias honors the same reservation.

I split the reroll into these two patches so that the branch-alias fix and the non-branch safeguards can be reviewed independently.

Thanks for the review, and sorry for the slow response.

Thanks, Son

On Thu, 4 Jun 2026 16:37:39 +0100, Phillip Wood <phillip.wood123@gmail.com> wrote:

Show 101 quoted lines
> On 03/06/2026 11:27, Son Luong Ngoc via GitGitGadget wrote:
> > From: Son Luong Ngoc <sluongng@gmail.com>
> >
> > git rebase --update-refs can fail after the normal rebase path has
> > updated the current branch when another local branch is a symref to it.
> > This can happen during a default-branch rename where refs/heads/main
> > points at refs/heads/master while users migrate.
> >
> > The sequencer queues update-ref commands from local branch decorations.
> > Commit 106b6885c7 (rebase: ignore non-branch update-refs) filters out
> > decorations that are not local branches, such as HEAD and tags. A branch
> > symref is different: it is still a local branch decoration, but if it
> > resolves to another branch then that target branch is itself present in
> > the decoration list and will be updated as a concrete branch.
> >
> > Skip branch decorations whose symrefs resolve to refs/heads/*, because
> > those targets are already represented by concrete branch decorations.
> > This prevents aliases from scheduling a second update for the same
> > branch. Keep symrefs to non-branch targets on the existing path.
>
> Makes sense
>
> > Preserve the existing checked-out branch handling before applying these
> > skips. Such refs still need a todo-list comment instead of an update-ref
> > command, even when the checked-out ref is the branch being rebased or a
> > branch symref alias. Use a copy of the resolved HEAD ref so later ref
> > resolution does not overwrite it.
>
> I don't quite understand this. A symref that points to another branch
> should always be skipped. When we look up which branches are checked out
> (see worktree.c:add_head_info()) we use
>
> refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
> "HEAD",
> 0,
> &wt->head_oid, &flags);
>
> so it will never report a symref as being checked out - it always
> resolves any symrefs first.
>
> If we have a symref pointing somewhere outside of "refs/heads" then we
> need to check whether the target is checked out, not the symref itself.
> I'm not sure how likely that is to happen in practice.
>
> > diff --git a/sequencer.c b/sequencer.c
> > index 1ee4b2875b..6ab8b47108 100644
> > --- a/sequencer.c
> > +++ b/sequencer.c
> > @@ -6445,28 +6445,46 @@ static int add_decorations_to_list(const struct commit *commit,
> > struct todo_add_branch_context *ctx)
> > {
> > const struct name_decoration *decoration = get_name_decoration(&commit->object);
> > - const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
> > - "HEAD",
> > - RESOLVE_REF_READING,
> > - NULL,
> > - NULL);
> > + struct ref_store *refs = get_main_ref_store(the_repository);
> > + char *head_ref = refs_resolve_refdup(refs, "HEAD",
> > + RESOLVE_REF_READING,
> > + NULL, NULL);
>
> This part and the test look good now
> > while (decoration) {
> > struct todo_item *item;
> > const char *path;
> > + const char *resolved_ref;
> > + int flags = 0;
> > size_t base_offset = ctx->buf->len;
> >
> > /*
> > - * If the branch is the current HEAD, then it will be
> > - * updated by the default rebase behavior.
> > - * Exclude it from the list of refs to update,
> > - * as well as any non-branch decorations.
> > * Non-branch decorations may be present if the pretty format
> > * includes "%d", which would have loaded all refs
> > * into the global decoration table.
> > */
> > - if ((head_ref && !strcmp(head_ref, decoration->name)) ||
> > - (decoration->type != DECORATION_REF_LOCAL)) {
> > + if (decoration->type != DECORATION_REF_LOCAL) {
> > + decoration = decoration->next;
> > + continue;
> > + }
>
> If a decoration matches the current branch why don't we just skip it
> like we used to? (As an aside the existing code in wrong because if the
> user runs "git rebase --update-refs <upstream> <branch>" HEAD does not
> point to "<branch>" but lets not worry about that now)
>
> > + path = branch_checked_out(decoration->name);
>
> As I said above if the symref target is anther branch we should skip it
> and if the target is not a branch then we need to check if the target is
> checked out so we need to resolve the ref before calling
> branch_checked_out().
>
> Thanks
>
> Phillip
Phillip WoodJul 23, 2026, 18:58 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 22/07/2026 09:15, Son Luong Ngoc via GitGitGadget wrote:
Show 24 quoted lines
> From: Son Luong Ngoc <sluongng@gmail.com>
> 
> git rebase --update-refs can finish rewriting the current branch and
> then fail while updating a local branch that is a symbolic ref. This can
> happen during a default-branch rename where refs/heads/main points at
> refs/heads/master while users migrate.
> 
> The problem is a partially applied ref update: the main rebase has
> already succeeded when the later ref update fails.
> 
> The sequencer queues updates from local branch decorations. Commit
> 106b6885c7 (rebase: ignore non-branch update-refs) filters out
> decorations such as HEAD and tags. A branch symref is still a local
> branch decoration, but refs_update_ref() dereferences it, so an alias to
> another branch duplicates the concrete branch update.
> 
> Resolve local branch decorations before queuing them. Skip symrefs whose
> targets are under refs/heads/ so that only the concrete branch update is
> queued. Keep an owned copy of the resolved HEAD and skip the current
> branch before checked-out handling so later ref resolution cannot change
> the comparison.
> 
> This prevents a successful rebase from being followed by a failed,
> partially applied ref update while preserving each alias as a symref.

Thanks for re-rolling I'm pretty sure the logic is sound now but I'm a bit confused by a couple of things - see my comments below.

Show 47 quoted lines
> Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
> ---
>   sequencer.c                   | 44 +++++++++++++++++++++++++----------
>   t/t3400-rebase.sh             |  2 +-
>   t/t3404-rebase-interactive.sh | 16 +++++++++++++
>   3 files changed, 49 insertions(+), 13 deletions(-)
> 
> diff --git a/sequencer.c b/sequencer.c
> index 1355a99a09..63aba60a08 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -6465,32 +6465,50 @@ static int add_decorations_to_list(const struct commit *commit,
>   				   struct todo_add_branch_context *ctx)
>   {
>   	const struct name_decoration *decoration = get_name_decoration(&commit->object);
> -	const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
> -						       "HEAD",
> -						       RESOLVE_REF_READING,
> -						       NULL,
> -						       NULL);
> +	struct ref_store *refs = get_main_ref_store(the_repository);
> +	char *head_ref = refs_resolve_refdup(refs, "HEAD",
> +					     RESOLVE_REF_READING,
> +					     NULL, NULL);
>   
>   	while (decoration) {
>   		struct todo_item *item;
>   		const char *path;
> +		char *resolved_ref;
> +		int flags = 0;
>   		size_t base_offset = ctx->buf->len;
>   
>   		/*
> -		 * If the branch is the current HEAD, then it will be
> -		 * updated by the default rebase behavior.
> -		 * Exclude it from the list of refs to update,
> -		 * as well as any non-branch decorations.
>   		 * Non-branch decorations may be present if the pretty format
>   		 * includes "%d", which would have loaded all refs
>   		 * into the global decoration table.
>   		 */
> -		if ((head_ref && !strcmp(head_ref, decoration->name)) ||
> -		    (decoration->type != DECORATION_REF_LOCAL)) {
> +		if (decoration->type != DECORATION_REF_LOCAL) {
> +			decoration = decoration->next;
> +			continue;
> +		}

It would be nice to have a comment here explaining what we're doing. Also I don't think we need to copy the refname so it would be more efficient to use refs_resolve_ref_unsafe().

Show 9 quoted lines
> +		resolved_ref = refs_resolve_refdup(refs, decoration->name,
> +						      RESOLVE_REF_READING,
> +						      NULL, &flags);
> +		if (resolved_ref && (flags & REF_ISSYMREF) &&
> +		    starts_with(resolved_ref, "refs/heads/")) {
> +			free(resolved_ref);
> +			decoration = decoration->next;
> +			continue;
> +		}
We skip any symbolic refs that point to another branch which is good.
Show 9 quoted lines
> +		/*
> +		 * If the branch is the current HEAD, then it will be
> +		 * updated by the default rebase behavior.
> +		 */
> +		if (head_ref && !strcmp(head_ref, decoration->name)) {
> +			free(resolved_ref);
>   			decoration = decoration->next;
>   			continue;
>   		}

Then we check to see if the decoration matches HEAD which we used to do above - I'm not clear why we have moved this check.

> +		path = branch_checked_out(decoration->name);
> +
This belongs in the next patch I think.
Show 5 quoted lines
> diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
> index e62e07b894..1a02f6546b 100755
> --- a/t/t3400-rebase.sh
> +++ b/t/t3400-rebase.sh
> @@ -471,7 +471,7 @@ test_expect_success 'git rebase --update-ref with core.commentChar and branch on
Adding an extra context line shows
	git checkout topic2>   	GIT_SEQUENCE_EDITOR="cat >actual" git -c 
core.commentChar=% \
>   		 rebase -i --update-refs base &&
>   	test_grep "% Ref refs/heads/wt-topic checked out at" actual &&
> -	test_grep "% Ref refs/heads/topic2 checked out at" actual
> +	test_grep ! "% Ref refs/heads/topic2 checked out at" actual

As topic2 is checked out in the worktree where the rebase is running why did this line appear before?

Show 9 quoted lines
> diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
> index e64816770a..11afa8be56 100755
> --- a/t/t3404-rebase-interactive.sh
> +++ b/t/t3404-rebase-interactive.sh
> @@ -1975,15 +1975,23 @@ test_expect_success '--update-refs ignores non-branch decorations' '
>   	) &&
>   	grep ^update-ref todo >actual &&
>   	test_write_lines "update-ref refs/heads/no-conflict-branch" >expect &&
> +	test_grep ! "^# Ref refs/heads/update-refs checked out" todo &&

Lets move this line below test_cmp so we keep that line next to the ones that create the files that are being compared. Is this another case where we used to add this comment and no longer do so?

Show 33 quoted lines
>   	test_cmp expect actual
>   '
>   
>   test_expect_success '--update-refs updates refs correctly' '
> +	test_when_finished "
> +		test_might_fail git symbolic-ref -d refs/heads/no-conflict-branch-alias &&
> +		test_might_fail git symbolic-ref -d refs/heads/second-alias
> +	" &&
>   	git checkout -B update-refs no-conflict-branch &&
>   	git branch -f base HEAD~4 &&
>   	git branch -f first HEAD~3 &&
>   	git branch -f second HEAD~3 &&
>   	git branch -f third HEAD~1 &&
> +	git symbolic-ref refs/heads/no-conflict-branch-alias \
> +		refs/heads/no-conflict-branch &&
> +	git symbolic-ref refs/heads/second-alias refs/heads/second &&
>   	test_commit extra2 fileX &&
>   	git commit --amend --fixup=L &&
>   
> @@ -1991,8 +1999,16 @@ test_expect_success '--update-refs updates refs correctly' '
>   
>   	test_cmp_rev HEAD~3 refs/heads/first &&
>   	test_cmp_rev HEAD~3 refs/heads/second &&
> +	test_cmp_rev HEAD~3 refs/heads/second-alias &&
>   	test_cmp_rev HEAD~1 refs/heads/third &&
>   	test_cmp_rev HEAD refs/heads/no-conflict-branch &&
> +	test_cmp_rev HEAD refs/heads/no-conflict-branch-alias &&
> +	test_write_lines refs/heads/no-conflict-branch >expect &&
> +	git symbolic-ref refs/heads/no-conflict-branch-alias >actual &&
> +	test_cmp expect actual &&
> +	test_write_lines refs/heads/second >expect &&
> +	git symbolic-ref refs/heads/second-alias >actual &&
> +	test_cmp expect actual &&

This looks good - we check that "rebase --update-refs" succeeds withh branches that are symrefs and also that those refs are untouched by the rebase.

Thanks
Phillip
>   	q_to_tab >expect <<-\EOF &&
>   	Successfully rebased and updated refs/heads/update-refs.
Phillip WoodJul 24, 2026, 09:55 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 23/07/2026 19:58, Phillip Wood wrote:
Show 81 quoted lines
> On 22/07/2026 09:15, Son Luong Ngoc via GitGitGadget wrote:
>> From: Son Luong Ngoc <sluongng@gmail.com>
>>
>> git rebase --update-refs can finish rewriting the current branch and
>> then fail while updating a local branch that is a symbolic ref. This can
>> happen during a default-branch rename where refs/heads/main points at
>> refs/heads/master while users migrate.
>>
>> The problem is a partially applied ref update: the main rebase has
>> already succeeded when the later ref update fails.
>>
>> The sequencer queues updates from local branch decorations. Commit
>> 106b6885c7 (rebase: ignore non-branch update-refs) filters out
>> decorations such as HEAD and tags. A branch symref is still a local
>> branch decoration, but refs_update_ref() dereferences it, so an alias to
>> another branch duplicates the concrete branch update.
>>
>> Resolve local branch decorations before queuing them. Skip symrefs whose
>> targets are under refs/heads/ so that only the concrete branch update is
>> queued. Keep an owned copy of the resolved HEAD and skip the current
>> branch before checked-out handling so later ref resolution cannot change
>> the comparison.
>>
>> This prevents a successful rebase from being followed by a failed,
>> partially applied ref update while preserving each alias as a symref.
> 
> Thanks for re-rolling I'm pretty sure the logic is sound now but I'm a 
> bit confused by a couple of things - see my comments below.
> 
>> Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>
>> ---
>>   sequencer.c                   | 44 +++++++++++++++++++++++++----------
>>   t/t3400-rebase.sh             |  2 +-
>>   t/t3404-rebase-interactive.sh | 16 +++++++++++++
>>   3 files changed, 49 insertions(+), 13 deletions(-)
>>
>> diff --git a/sequencer.c b/sequencer.c
>> index 1355a99a09..63aba60a08 100644
>> --- a/sequencer.c
>> +++ b/sequencer.c
>> @@ -6465,32 +6465,50 @@ static int add_decorations_to_list(const 
>> struct commit *commit,
>>                      struct todo_add_branch_context *ctx)
>>   {
>>       const struct name_decoration *decoration = 
>> get_name_decoration(&commit->object);
>> -    const char *head_ref = 
>> refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
>> -                               "HEAD",
>> -                               RESOLVE_REF_READING,
>> -                               NULL,
>> -                               NULL);
>> +    struct ref_store *refs = get_main_ref_store(the_repository);
>> +    char *head_ref = refs_resolve_refdup(refs, "HEAD",
>> +                         RESOLVE_REF_READING,
>> +                         NULL, NULL);
>>       while (decoration) {
>>           struct todo_item *item;
>>           const char *path;
>> +        char *resolved_ref;
>> +        int flags = 0;
>>           size_t base_offset = ctx->buf->len;
>>           /*
>> -         * If the branch is the current HEAD, then it will be
>> -         * updated by the default rebase behavior.
>> -         * Exclude it from the list of refs to update,
>> -         * as well as any non-branch decorations.
>>            * Non-branch decorations may be present if the pretty format
>>            * includes "%d", which would have loaded all refs
>>            * into the global decoration table.
>>            */
>> -        if ((head_ref && !strcmp(head_ref, decoration->name)) ||
>> -            (decoration->type != DECORATION_REF_LOCAL)) {
>> +        if (decoration->type != DECORATION_REF_LOCAL) {
>> +            decoration = decoration->next;
>> +            continue;
>> +        }
> 
> It would be nice to have a comment here explaining what we're doing. 
> Also I don't think we need to copy the refname so it would be more 
> efficient to use refs_resolve_ref_unsafe().

Looking at this again we cannot use refs_resolve_ref_unsafe() because the result would be overwritten by the call to refs_resolve_refdup() in branch_checked_out().

Show 24 quoted lines
>> +        resolved_ref = refs_resolve_refdup(refs, decoration->name,
>> +                              RESOLVE_REF_READING,
>> +                              NULL, &flags);
>> +        if (resolved_ref && (flags & REF_ISSYMREF) &&
>> +            starts_with(resolved_ref, "refs/heads/")) {
>> +            free(resolved_ref);
>> +            decoration = decoration->next;
>> +            continue;
>> +        }
> 
> We skip any symbolic refs that point to another branch which is good.
> 
>> +        /*
>> +         * If the branch is the current HEAD, then it will be
>> +         * updated by the default rebase behavior.
>> +         */
>> +        if (head_ref && !strcmp(head_ref, decoration->name)) {
>> +            free(resolved_ref);
>>               decoration = decoration->next;
>>               continue;
>>           }
> 
> Then we check to see if the decoration matches HEAD which we used to do 
> above - I'm not clear why we have moved this check.

Should we be using "resolved_ref" instead of "decoration->name"? That would explain why this was moved and would makes sense as we resolve symrefs when reading HEAD. When HEAD points outside "refs/heads/" we'd then skip updating any symrefs under "refs/heads/" that pointed to the same ref as HEAD.

Thanks
Phillip
Show 86 quoted lines
> 
>> +        path = branch_checked_out(decoration->name);
>> +
> 
> This belongs in the next patch I think.
> 
>> diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
>> index e62e07b894..1a02f6546b 100755
>> --- a/t/t3400-rebase.sh
>> +++ b/t/t3400-rebase.sh
>> @@ -471,7 +471,7 @@ test_expect_success 'git rebase --update-ref with 
>> core.commentChar and branch on
> 
> Adding an extra context line shows
> 
>      git checkout topic2>       GIT_SEQUENCE_EDITOR="cat >actual" git -c 
> core.commentChar=% \
>>            rebase -i --update-refs base &&
>>       test_grep "% Ref refs/heads/wt-topic checked out at" actual &&
>> -    test_grep "% Ref refs/heads/topic2 checked out at" actual
>> +    test_grep ! "% Ref refs/heads/topic2 checked out at" actual
> 
> As topic2 is checked out in the worktree where the rebase is running why 
> did this line appear before?
> 
>> diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase- 
>> interactive.sh
>> index e64816770a..11afa8be56 100755
>> --- a/t/t3404-rebase-interactive.sh
>> +++ b/t/t3404-rebase-interactive.sh
>> @@ -1975,15 +1975,23 @@ test_expect_success '--update-refs ignores 
>> non-branch decorations' '
>>       ) &&
>>       grep ^update-ref todo >actual &&
>>       test_write_lines "update-ref refs/heads/no-conflict-branch" 
>> >expect &&
>> +    test_grep ! "^# Ref refs/heads/update-refs checked out" todo &&
> 
> Lets move this line below test_cmp so we keep that line next to the ones 
> that create the files that are being compared. Is this another case 
> where we used to add this comment and no longer do so?
> 
>>       test_cmp expect actual
>>   '
>>   test_expect_success '--update-refs updates refs correctly' '
>> +    test_when_finished "
>> +        test_might_fail git symbolic-ref -d refs/heads/no-conflict- 
>> branch-alias &&
>> +        test_might_fail git symbolic-ref -d refs/heads/second-alias
>> +    " &&
>>       git checkout -B update-refs no-conflict-branch &&
>>       git branch -f base HEAD~4 &&
>>       git branch -f first HEAD~3 &&
>>       git branch -f second HEAD~3 &&
>>       git branch -f third HEAD~1 &&
>> +    git symbolic-ref refs/heads/no-conflict-branch-alias \
>> +        refs/heads/no-conflict-branch &&
>> +    git symbolic-ref refs/heads/second-alias refs/heads/second &&
>>       test_commit extra2 fileX &&
>>       git commit --amend --fixup=L &&
>> @@ -1991,8 +1999,16 @@ test_expect_success '--update-refs updates refs 
>> correctly' '
>>       test_cmp_rev HEAD~3 refs/heads/first &&
>>       test_cmp_rev HEAD~3 refs/heads/second &&
>> +    test_cmp_rev HEAD~3 refs/heads/second-alias &&
>>       test_cmp_rev HEAD~1 refs/heads/third &&
>>       test_cmp_rev HEAD refs/heads/no-conflict-branch &&
>> +    test_cmp_rev HEAD refs/heads/no-conflict-branch-alias &&
>> +    test_write_lines refs/heads/no-conflict-branch >expect &&
>> +    git symbolic-ref refs/heads/no-conflict-branch-alias >actual &&
>> +    test_cmp expect actual &&
>> +    test_write_lines refs/heads/second >expect &&
>> +    git symbolic-ref refs/heads/second-alias >actual &&
>> +    test_cmp expect actual &&
> 
> This looks good - we check that "rebase --update-refs" succeeds withh 
> branches that are symrefs and also that those refs are untouched by the 
> rebase.
> 
> Thanks
> 
> Phillip
> 
>>       q_to_tab >expect <<-\EOF &&
>>       Successfully rebased and updated refs/heads/update-refs.
> 
Erik Cervin-EdinJul 25, 2026, 11:21 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

I ran into a minor regression that I tracked down to v2 of this patch series. When I was running

    git rebase --interactive --update-refs

I got a comment in my git-rebase-todo for the branch I was rebasing, even though it's just the current branch in the current worktree:

    # Ref refs/heads/main checked out at '/private/tmp/gittest'

where I'd expect no entry at all, since the branch is updated by the rebase itself. After applying v3 of the series, the regression went away. Nevertheless, I thought I ought to share what I found.

On 26/07/24 10:55AM, Phillip Wood wrote:
Show 10 quoted lines
> > > +        if (head_ref && !strcmp(head_ref, decoration->name)) {
> > > +            free(resolved_ref);
> > >               decoration = decoration->next;
> > >               continue;
> > >           }
> > > +
> > > +        path = branch_checked_out(decoration->name);
> >
> > Then we check to see if the decoration matches HEAD which we used to do
> > above - I'm not clear why we have moved this check.

branch_checked_out() can't tell "checked out in another worktree" apart from "checked out right here", so `path` is never NULL for the branch actually being rebased. In v2, the check above was instead:

    if (!path && head_ref && !strcmp(head_ref, decoration->name))
        continue;

which made it a no-op for exactly that branch -- the regression I observed. v3 drops the `!path` gate and moves `path` below it, which is why it's fixed.

> > As topic2 is checked out in the worktree where the rebase is running
> > why did this line appear before?

This might be the same symptom from another cause: on master, head_ref comes from refs_resolve_ref_unsafe(), and as you note its buffer is overwritten inside branch_checked_out(). So by the time topic2 is compared, wt-topic's lookup may already have clobbered head_ref, letting topic2 fall through to the comment. I haven't run the test to confirm, though.

Thanks, Erik

Junio C HamanoJul 26, 2026, 15:42 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 10 quoted lines
>> Thanks for re-rolling I'm pretty sure the logic is sound now but I'm a 
>> bit confused by a couple of things - see my comments below.
>> ...
>> It would be nice to have a comment here explaining what we're doing. 
>> Also I don't think we need to copy the refname so it would be more 
>> efficient to use refs_resolve_ref_unsafe().
>
> Looking at this again we cannot use refs_resolve_ref_unsafe() because 
> the result would be overwritten by the call to refs_resolve_refdup() in 
> branch_checked_out().

Makes sense. Thanks for raising a possible alternative and then clarifying that it is not quite workable.

Show 18 quoted lines
>>> +        /*
>>> +         * If the branch is the current HEAD, then it will be
>>> +         * updated by the default rebase behavior.
>>> +         */
>>> +        if (head_ref && !strcmp(head_ref, decoration->name)) {
>>> +            free(resolved_ref);
>>>               decoration = decoration->next;
>>>               continue;
>>>           }
>> 
>> Then we check to see if the decoration matches HEAD which we used to do 
>> above - I'm not clear why we have moved this check.
>
> Should we be using "resolved_ref" instead of "decoration->name"? That 
> would explain why this was moved and would makes sense as we resolve 
> symrefs when reading HEAD. When HEAD points outside "refs/heads/" we'd 
> then skip updating any symrefs under "refs/heads/" that pointed to the 
> same ref as HEAD.

Yeah, decoration is very much end-user facing and if we can make behavioural decision based on a more stable resolved_ref that would make it easier to reason about.

But stepping back a bit, is having a HEAD that is a symref and points outside "refs/heads/" an invalid state? Why are we catering to such a configuration to begin with?

Phillip WoodJul 28, 2026, 09:36 UTC in reply to Junio C Hamano on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 26/07/2026 16:42, Junio C Hamano wrote:
Show 42 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>>> Thanks for re-rolling I'm pretty sure the logic is sound now but I'm a
>>> bit confused by a couple of things - see my comments below.
>>> ...
>>> It would be nice to have a comment here explaining what we're doing.
>>> Also I don't think we need to copy the refname so it would be more
>>> efficient to use refs_resolve_ref_unsafe().
>>
>> Looking at this again we cannot use refs_resolve_ref_unsafe() because
>> the result would be overwritten by the call to refs_resolve_refdup() in
>> branch_checked_out().
> 
> Makes sense.  Thanks for raising a possible alternative and then
> clarifying that it is not quite workable.
> 
>>>> +        /*
>>>> +         * If the branch is the current HEAD, then it will be
>>>> +         * updated by the default rebase behavior.
>>>> +         */
>>>> +        if (head_ref && !strcmp(head_ref, decoration->name)) {
>>>> +            free(resolved_ref);
>>>>                decoration = decoration->next;
>>>>                continue;
>>>>            }
>>>
>>> Then we check to see if the decoration matches HEAD which we used to do
>>> above - I'm not clear why we have moved this check.
>>
>> Should we be using "resolved_ref" instead of "decoration->name"? That
>> would explain why this was moved and would makes sense as we resolve
>> symrefs when reading HEAD. When HEAD points outside "refs/heads/" we'd
>> then skip updating any symrefs under "refs/heads/" that pointed to the
>> same ref as HEAD.
> 
> Yeah, decoration is very much end-user facing and if we can make
> behavioural decision based on a more stable resolved_ref that would
> make it easier to reason about.
> 
> But stepping back a bit, is having a HEAD that is a symref and
> points outside "refs/heads/" an invalid state?  Why are we catering
> to such a configuration to begin with?

We allow HEAD to point to anything below "refs/" - see e9cc02f0e4 (symbolic-ref: allow refs/<whatever> in HEAD, 2009-02-13). I've not tested it but looking at the code I think rebase handles a non-branch HEAD correctly.

It would be nice if we didn't have to worry about non-branch HEADs, recently Caleb reported on discord being surprised that git allows HEAD to point to a tag.

Thanks
Phillip
Junio C HamanoJul 28, 2026, 14:23 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 6 quoted lines
>> But stepping back a bit, is having a HEAD that is a symref and
>> points outside "refs/heads/" an invalid state?  Why are we catering
>> to such a configuration to begin with?
>
> We allow HEAD to point to anything below "refs/" - see e9cc02f0e4 
> (symbolic-ref: allow refs/<whatever> in HEAD, 2009-02-13).

But that was about a low level mechanism that must be more lenient to be usable as repair tools to recover from such a broken state, no? I thought the end-user facing commands like "git checkout" have been tightened long ago to stop users from getting into a situation that needs repairing in the first place. And that was why I asked.

Phillip WoodJul 29, 2026, 09:31 UTC in reply to Junio C Hamano on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 28/07/2026 15:23, Junio C Hamano wrote:
Show 12 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>>> But stepping back a bit, is having a HEAD that is a symref and
>>> points outside "refs/heads/" an invalid state?  Why are we catering
>>> to such a configuration to begin with?
>>
>> We allow HEAD to point to anything below "refs/" - see e9cc02f0e4
>> (symbolic-ref: allow refs/<whatever> in HEAD, 2009-02-13).
> 
> But that was about a low level mechanism that must be more lenient
> to be usable as repair tools to recover from such a broken state,
> no?

It checks the new value of HEAD, not the old one so I don't think so. The commit message talks about topgit using "git symbolic-ref" to set head outside "refs/heads/" - peff had previously tried to tighten it to reject non-branch refs but that broke topgit. I've just had a quick look at the topgit code and still sets HEAD to point to "refs/top-bases/..." by default[1], although there are plans to start using "refs/heads/{top-bases}/..." instead.

>  I thought the end-user facing commands like "git checkout" have
> been tightened long ago to stop users from getting into a situation
> that needs repairing in the first place.  And that was why I asked.

Yes "git checkout" detaches HEAD if you give a non-branch ref, but external tools can still use "git symbolic-ref" to bypass that. It appears topgit's rebase command is built around "git rebase"[2] so I think we need to continue to support rebasing a non-branch HEAD.

Thanks
Phillip

[1] https://github.com/mackyle/topgit/blob/master/tg.sh#L2683 [2] https://github.com/mackyle/topgit/blob/master/tg-rebase.sh#L56

Junio C HamanoJul 29, 2026, 14:26 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 11 quoted lines
>> But that was about a low level mechanism that must be more lenient
>> to be usable as repair tools to recover from such a broken state,
>> no?
>
> It checks the new value of HEAD, not the old one so I don't think so. 
> The commit message talks about topgit using "git symbolic-ref" to set 
> head outside "refs/heads/" - peff had previously tried to tighten it to 
> reject non-branch refs but that broke topgit. I've just had a quick look 
> at the topgit code and still sets HEAD to point to "refs/top-bases/..." 
> by default[1], although there are plans to start using 
> "refs/heads/{top-bases}/..." instead.

Ah, that name vaguely rings a bell. Is it still in use, and now they prevent us from forbidding funny characters like {} in the refname? Sigh...

> ... It 
> appears topgit's rebase command is built around "git rebase"[2] so I 
> think we need to continue to support rebasing a non-branch HEAD.
Sigh, again, but OK.
Thanks.
Phillip WoodJul 30, 2026, 13:10 UTC in reply to Junio C Hamano on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 29/07/2026 15:26, Junio C Hamano wrote:
Show 17 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>>> But that was about a low level mechanism that must be more lenient
>>> to be usable as repair tools to recover from such a broken state,
>>> no?
>>
>> It checks the new value of HEAD, not the old one so I don't think so.
>> The commit message talks about topgit using "git symbolic-ref" to set
>> head outside "refs/heads/" - peff had previously tried to tighten it to
>> reject non-branch refs but that broke topgit. I've just had a quick look
>> at the topgit code and still sets HEAD to point to "refs/top-bases/..."
>> by default[1], although there are plans to start using
>> "refs/heads/{top-bases}/..." instead.
> 
> Ah, that name vaguely rings a bell.  Is it still in use, and now
> they prevent us from forbidding funny characters like {} in the
> refname?  Sigh...

Yes, it still seems to be maintained, I guess they chose the funny characters to try and avoid name collisions because no-one would want them in a "normal" branch name.

Thanks
Phillip
Show 8 quoted lines
>> ... It
>> appears topgit's rebase command is built around "git rebase"[2] so I
>> think we need to continue to support rebasing a non-branch HEAD.
> 
> Sigh, again, but OK.
> 
> Thanks.
> 
Junio C HamanoAug 6, 2026, 20:12 UTC in reply to Phillip Wood on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 24 quoted lines
> On 29/07/2026 15:26, Junio C Hamano wrote:
>> Phillip Wood <phillip.wood123@gmail.com> writes:
>> 
>>>> But that was about a low level mechanism that must be more lenient
>>>> to be usable as repair tools to recover from such a broken state,
>>>> no?
>>>
>>> It checks the new value of HEAD, not the old one so I don't think so.
>>> The commit message talks about topgit using "git symbolic-ref" to set
>>> head outside "refs/heads/" - peff had previously tried to tighten it to
>>> reject non-branch refs but that broke topgit. I've just had a quick look
>>> at the topgit code and still sets HEAD to point to "refs/top-bases/..."
>>> by default[1], although there are plans to start using
>>> "refs/heads/{top-bases}/..." instead.
>> 
>> Ah, that name vaguely rings a bell.  Is it still in use, and now
>> they prevent us from forbidding funny characters like {} in the
>> refname?  Sigh...
>
> Yes, it still seems to be maintained, I guess they chose the funny 
> characters to try and avoid name collisions because no-one would want 
> them in a "normal" branch name.
>
> Thanks

I guess I dropped the ball here. So given that we are OK to see HEAD pointing outside refs/heads/, this subthread should be closed.

My understanding is that we still have an issue reported by Erik in <amSSYagL0jTgzElD@mbp> that needs to be addressed before this topic can move further?

Thanks.
Phillip WoodAug 7, 2026, 15:22 UTC in reply to Son Luong Ngoc via GitGitGadget on lore

Re: [PATCH v3 2/2] rebase: guard non-branch symref targets

On 22/07/2026 09:15, Son Luong Ngoc via GitGitGadget wrote:
Show 19 quoted lines
> From: Son Luong Ngoc <sluongng@gmail.com>
> 
> A local branch symbolic ref may point outside refs/heads/. Such an alias
> cannot be skipped like a branch-to-branch alias because its concrete
> target ref is absent from the local branch decoration list.
> 
> However, queuing each alias independently can update the same target ref
> more than once and make the second compare-and-swap fail. A reservation
> from another worktree can also name either an alias or its resolved
> target ref, so checking only one form can miss an in-progress update.
> 
> Fix these cases by checking both the literal alias and its resolved
> target ref against checked-out reservations. Deduplicate updates by
> target ref. Also reserve both forms when loading another worktree's
> update-refs state. This makes different aliases honor the same
> in-progress update.
> 
> This keeps non-branch symrefs supported without allowing duplicate or
> cross-worktree ref updates.

I've left a couple of questions below. If you're short on time I'd suggest we just concentrate on getting the first patch merged as that fixes the common case of one branch being a symlink to another. We can worry about two branches being symlinks to a ref outside "refs/heads/*" later if it turns out to be a problem in practice.

Show 28 quoted lines
> diff --git a/branch.c b/branch.c
> index 243db7d0fc..98a50d8368 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -442,10 +442,25 @@ static void prepare_checked_out_branches(void)
>   						     &update_refs)) {
>   			struct string_list_item *item;
>   			for_each_string_list_item(item, &update_refs) {
> +				char *resolved_ref;
> +				int flags = 0;
> +
>   				old = strmap_put(&current_checked_out_branches,
>   						 item->string,
>   						 xstrdup(wt->path));
>   				free(old);
> +
> +				resolved_ref = refs_resolve_refdup(
> +					get_main_ref_store(the_repository),
> +					item->string, RESOLVE_REF_READING,
> +					NULL, &flags);
> +				if (resolved_ref && (flags & REF_ISSYMREF)) {
> +					old = strmap_put(
> +						&current_checked_out_branches,
> +						resolved_ref, xstrdup(wt->path));
> +					free(old);
> +				}
> +				free(resolved_ref);
>   			}

After the last commit, when we prepare the todo list don't we skip any symbolic refs and only record their target? That would mean there shouldn't be any symbolic refs to resolve here. I do wonder if the earlier part of this function should be storing the symref and its target when it walks all the worktree HEADs. If we have a branch "refs/heads/feature" and a symref "refs/heads/symlink-to-feature" is it possible to have them checkedout in different worktrees because we only add HEAD to the list of checked out branches when we walk all the worktree HEADs?

Show 37 quoted lines
>   			string_list_clear(&update_refs, 1);
>   		}
> diff --git a/sequencer.c b/sequencer.c
> index 63aba60a08..040b5bf645 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -6459,6 +6459,7 @@ struct todo_add_branch_context {
>   	size_t items_alloc;
>   	struct strbuf *buf;
>   	struct string_list refs_to_oids;
> +	struct string_list symref_update_targets;
>   };
>   
>   static int add_decorations_to_list(const struct commit *commit,
> @@ -6473,6 +6474,7 @@ static int add_decorations_to_list(const struct commit *commit,
>   	while (decoration) {
>   		struct todo_item *item;
>   		const char *path;
> +		const char *checked_ref;
>   		char *resolved_ref;
>   		int flags = 0;
>   		size_t base_offset = ctx->buf->len;
> @@ -6508,6 +6510,17 @@ static int add_decorations_to_list(const struct commit *commit,
>   		}
>   
>   		path = branch_checked_out(decoration->name);
> +		if (!path && resolved_ref && (flags & REF_ISSYMREF)) {
> +			checked_ref = resolved_ref;
> +			path = branch_checked_out(checked_ref);
> +		}
> +		if (!path && resolved_ref && (flags & REF_ISSYMREF) &&
> +		    string_list_has_string(&ctx->symref_update_targets,
> +					   resolved_ref)) {
> +			free(resolved_ref);
> +			decoration = decoration->next;
> +			continue;
> +		}

So we check the to see if the symref or its target are checked out. That's necessary because we might have stored a symref rather than its target in current_checked_out_branches above (which I think is probably a bug). If two branches are symrefs to the same ref we'll only queue the update once which is good.

Thanks
Phillip
Show 112 quoted lines
>   		ALLOC_GROW(ctx->items,
>   			ctx->items_nr + 1,
> @@ -6523,6 +6536,10 @@ static int add_decorations_to_list(const struct commit *commit,
>   					      decoration->name, path);
>   		} else {
>   			struct string_list_item *sti;
> +
> +			if (resolved_ref && (flags & REF_ISSYMREF))
> +				string_list_insert(&ctx->symref_update_targets,
> +						   resolved_ref);
>   			item->command = TODO_UPDATE_REF;
>   			strbuf_addf(ctx->buf, "%s\n", decoration->name);
>   
> @@ -6554,6 +6571,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
>   	struct todo_add_branch_context ctx = {
>   		.buf = &todo_list->buf,
>   		.refs_to_oids = STRING_LIST_INIT_DUP,
> +		.symref_update_targets = STRING_LIST_INIT_DUP,
>   	};
>   
>   	ctx.items_alloc = 2 * todo_list->nr + 1;
> @@ -6579,6 +6597,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
>   	res = write_update_refs_state(&ctx.refs_to_oids);
>   
>   	string_list_clear(&ctx.refs_to_oids, 1);
> +	string_list_clear(&ctx.symref_update_targets, 0);
>   
>   	if (res) {
>   		/* we failed, so clean up the new list. */
> diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
> index 11afa8be56..110ed8ae63 100755
> --- a/t/t3404-rebase-interactive.sh
> +++ b/t/t3404-rebase-interactive.sh
> @@ -2024,6 +2024,78 @@ test_expect_success '--update-refs updates refs correctly' '
>   	test_cmp expect err.trimmed
>   '
>   
> +test_expect_success '--update-refs checks resolved non-branch symref target' '
> +	test_when_finished "
> +		git worktree remove --force checked-out-target-wt &&
> +		git symbolic-ref -d refs/heads/non-branch-alias &&
> +		git tag -d checked-out-target
> +	" &&
> +	git tag checked-out-target HEAD~1 &&
> +	git symbolic-ref refs/heads/non-branch-alias refs/tags/checked-out-target &&
> +	git worktree add --detach checked-out-target-wt checked-out-target &&
> +	git -C checked-out-target-wt symbolic-ref HEAD refs/tags/checked-out-target &&
> +
> +	GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 &&
> +
> +	test_grep "^# Ref refs/heads/non-branch-alias checked out at" todo &&
> +	test_write_lines refs/tags/checked-out-target >expect &&
> +	git symbolic-ref refs/heads/non-branch-alias >actual &&
> +	test_cmp expect actual
> +'
> +
> +test_expect_success '--update-refs deduplicates non-branch symref targets' '
> +	test_when_finished "
> +		git symbolic-ref -d refs/heads/non-branch-alias-one &&
> +		git symbolic-ref -d refs/heads/non-branch-alias-two &&
> +		git tag -d shared-non-branch-target
> +	" &&
> +	git tag shared-non-branch-target HEAD~1 &&
> +	git symbolic-ref refs/heads/non-branch-alias-one \
> +		refs/tags/shared-non-branch-target &&
> +	git symbolic-ref refs/heads/non-branch-alias-two \
> +		refs/tags/shared-non-branch-target &&
> +
> +	GIT_SEQUENCE_EDITOR=: git rebase -i --force-rebase --update-refs HEAD~2 &&
> +
> +	test_cmp_rev HEAD~1 refs/heads/non-branch-alias-one &&
> +	test_cmp_rev HEAD~1 refs/heads/non-branch-alias-two &&
> +	test_write_lines refs/tags/shared-non-branch-target >expect &&
> +	git symbolic-ref refs/heads/non-branch-alias-one >actual &&
> +	test_cmp expect actual &&
> +	git symbolic-ref refs/heads/non-branch-alias-two >actual &&
> +	test_cmp expect actual
> +'
> +
> +test_expect_success '--update-refs honors non-branch symref reservations' '
> +	test_when_finished "
> +		test_might_fail git worktree remove --force reserved-target-wt &&
> +		test_might_fail git symbolic-ref -d \
> +			refs/heads/reserved-non-branch-alias-one &&
> +		test_might_fail git symbolic-ref -d \
> +			refs/heads/reserved-non-branch-alias-two &&
> +		test_might_fail git tag -d reserved-non-branch-target
> +	" &&
> +	git tag reserved-non-branch-target HEAD~1 &&
> +	git symbolic-ref refs/heads/reserved-non-branch-alias-one \
> +		refs/tags/reserved-non-branch-target &&
> +	git symbolic-ref refs/heads/reserved-non-branch-alias-two \
> +		refs/tags/reserved-non-branch-target &&
> +	git worktree add --detach reserved-target-wt HEAD &&
> +	wt_gitdir=$(git -C reserved-target-wt rev-parse --absolute-git-dir) &&
> +	mkdir -p "$wt_gitdir/rebase-merge" &&
> +	old_oid=$(git rev-parse refs/heads/reserved-non-branch-alias-one) &&
> +	test_write_lines refs/heads/reserved-non-branch-alias-one \
> +		"$old_oid" "$old_oid" >"$wt_gitdir/rebase-merge/update-refs" &&
> +
> +	GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 &&
> +
> +	test_grep "^# Ref refs/heads/reserved-non-branch-alias-one checked out at" \
> +		todo &&
> +	test_grep "^# Ref refs/heads/reserved-non-branch-alias-two checked out at" \
> +		todo &&
> +	test_grep ! "^update-ref refs/heads/reserved-non-branch-alias" todo
> +'
> +
>   test_expect_success 'respect user edits to update-ref steps' '
>   	git checkout -B update-refs-break no-conflict-branch &&
>   	git branch -f base HEAD~4 &&
Phillip WoodAug 7, 2026, 15:28 UTC in reply to Junio C Hamano on lore

Re: [PATCH v3 1/2] rebase: skip branch symref aliases

On 06/08/2026 21:12, Junio C Hamano wrote:
Show 33 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> On 29/07/2026 15:26, Junio C Hamano wrote:
>>> Phillip Wood <phillip.wood123@gmail.com> writes:
>>>
>>>>> But that was about a low level mechanism that must be more lenient
>>>>> to be usable as repair tools to recover from such a broken state,
>>>>> no?
>>>>
>>>> It checks the new value of HEAD, not the old one so I don't think so.
>>>> The commit message talks about topgit using "git symbolic-ref" to set
>>>> head outside "refs/heads/" - peff had previously tried to tighten it to
>>>> reject non-branch refs but that broke topgit. I've just had a quick look
>>>> at the topgit code and still sets HEAD to point to "refs/top-bases/..."
>>>> by default[1], although there are plans to start using
>>>> "refs/heads/{top-bases}/..." instead.
>>>
>>> Ah, that name vaguely rings a bell.  Is it still in use, and now
>>> they prevent us from forbidding funny characters like {} in the
>>> refname?  Sigh...
>>
>> Yes, it still seems to be maintained, I guess they chose the funny
>> characters to try and avoid name collisions because no-one would want
>> them in a "normal" branch name.
>>
>> Thanks
> 
> I guess I dropped the ball here.  So given that we are OK to see
> HEAD pointing outside refs/heads/, this subthread should be closed.
> 
> My understanding is that we still have an issue reported by Erik in
> <amSSYagL0jTgzElD@mbp> that needs to be addressed before this topic
> can move further?

Maybe I misunderstood but I thought Erik was pointing out a bug in v2 that was fixed in v3. I do think we want a re-roll though for

 >> +        /*
 >> +         * If the branch is the current HEAD, then it will be
 >> +         * updated by the default rebase behavior.
 >> +         */
 >> +        if (head_ref && !strcmp(head_ref, decoration->name)) {
 >> +            free(resolved_ref);
 >>               decoration = decoration->next;
 >>               continue;
 >>           }
 > [...]
 > Should we be using "resolved_ref" instead of "decoration->name"? That
 > would explain why this was moved and would makes sense as we resolve
 > symrefs when reading HEAD. When HEAD points outside "refs/heads/" 
we'd > then skip updating any symrefs under "refs/heads/" that pointed 
to the > same ref as HEAD.
in [1]. I've also just left some comments on the second patch
Thanks
Phillip

[1] https://lore.kernel.org/git/00e529b6-7ae7-463f-a4b3-0991e9411aba@gmail.com

Back to recent threads