git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/3] branch: strip refs/heads/ from the --set-upstream-to hint

From
TGtsukasa inoue via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 21, 2026, 21:35 UTC
Message-ID
<e7059183786a5983bdebda34b3eb8f8f822ca9c0.1792618518.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2268.v2.git.1792618518.gitgitgadget@gmail.com>
From: tsukasa inoue <inoue.tsukasa.4@gmail.com>

When git switch fails to write the tracking config, it prints a hint with the command to set the upstream by hand. For example, if .git/config.lock exists:

    error: could not lock config file .git/config: File exists
    error: unable to write upstream branch configuration
    hint:
    hint: After fixing the error cause you may try to fix up
    hint: the remote tracking information by invoking:
    hint:   git branch --set-upstream-to=origin/refs/heads/feat

The hint says origin/refs/heads/feat, but the correct name is origin/feat. Running the command as printed fails, because the full ref name is not a valid remote-tracking branch name here.

This has been wrong since a3f40ec4b0 (branch: accept multiple upstream branches for tracking, 2021-12-20).

Strip refs/heads/ from the hint, as the success message in the same function already does. The git config --add line for multiple upstreams still needs the full ref name, so it is not changed.

Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>
---
 branch.c          |  8 +++++---
 t/t3200-branch.sh | 25 +++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/branch.c b/branch.c
index 22f4f46b96..bb07846911 100644
--- a/branch.c
+++ b/branch.c
@@ -186,12 +186,14 @@ out_err:
 
 	advise(_("\nAfter fixing the error cause you may try to fix up\n"
 		"the remote tracking information by invoking:"));
-	if (remotes->nr == 1)
+	if (remotes->nr == 1) {
+		shortname = remotes->items[0].string;
+		skip_prefix(shortname, "refs/heads/", &shortname);
 		advise("  git branch --set-upstream-to=%s%s%s",
 			origin ? origin : "",
 			origin ? "/" : "",
-			remotes->items[0].string);
-	else {
+			shortname);
+	} else {
 		advise("  git config --add branch.\"%s\".remote %s",
 			local, origin ? origin : ".");
 		for_each_string_list_item(item, remotes)
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index cdb6c6a634..ccfb53efa3 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1080,6 +1080,31 @@ test_expect_success '--set-upstream-to fails on locked config' '
 	test_grep "could not lock config file .git/config" err
 '
 
+test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '
+	test_when_finished "git branch -D hint-local" &&
+	test_when_finished "git update-ref -d refs/remotes/hintremote/hintbranch" &&
+	test_config remote.hintremote.url . &&
+	test_config remote.hintremote.fetch "+refs/heads/*:refs/remotes/hintremote/*" &&
+	git update-ref refs/remotes/hintremote/hintbranch HEAD &&
+	git branch hint-local &&
+	test_when_finished "rm -f .git/config.lock" &&
+	>.git/config.lock &&
+	test_must_fail git -c core.configLockTimeout=0 \
+		branch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&
+	cat >expect <<-\EOF &&
+	hint:
+	hint: After fixing the error cause you may try to fix up
+	hint: the remote tracking information by invoking:
+	hint:   git branch --set-upstream-to=hintremote/hintbranch
+	EOF
+	grep "^hint:" err >actual &&
+	test_cmp expect actual &&
+	rm -f .git/config.lock &&
+	git branch --set-upstream-to=hintremote/hintbranch hint-local &&
+	test_cmp_config hintremote branch.hint-local.remote &&
+	test_cmp_config refs/heads/hintbranch branch.hint-local.merge
+'
+
 test_expect_success 'use --set-upstream-to modify HEAD' '
 	test_config branch.main.remote foo &&
 	test_config branch.main.merge foo &&
-- 
gitgitgadget
Previous: tsukasa-ino via GitGitGadgetNext: tsukasa inoue via GitGitGadget
Message 5 of 9 in “branch: strip refs/heads/ from the --set-upstream-to hint”
  1. branch: strip refs/heads/ from the --set-upstream-to hinttsukasa-ino via GitGitGadget, Oct 20, 2026
  2. Junio C HamanoOct 20, 2026
  3. Harald NordgrenOct 21, 2026
  4. 0/3 checkout: finish switch when tracking config cannot be writtentsukasa-ino via GitGitGadget, Oct 21, 2026
  5. 1/3 branch: strip refs/heads/ from the --set-upstream-to hinttsukasa inoue via GitGitGadget, Oct 21, 2026
  6. 2/3 branch: return an error from setup_tracking()tsukasa inoue via GitGitGadget, Oct 21, 2026
  7. 3/3 checkout: finish switching when the tracking config cannot be writtentsukasa inoue via GitGitGadget, Oct 21, 2026
  8. Junio C HamanoOct 21, 2026
  9. 井上司Oct 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.