Git development
 help / color / mirror / Atom feed
From: "tsukasa-ino via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: tsukasa-ino <inoue.tsukasa.4@gmail.com>,
	tsukasa inoue <inoue.tsukasa.4@gmail.com>
Subject: [PATCH] branch: strip refs/heads/ from the --set-upstream-to hint
Date: Tue, 20 Oct 2026 01:15:04 +0000	[thread overview]
Message-ID: <pull.2268.git.1792458904027.gitgitgadget@gmail.com> (raw)

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: strip refs/heads/ from the --set-upstream-to hint

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2268%2FtsukasaI%2Ffix%2Fbranch-upstream-hint-shortname-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2268

 branch.c          |  8 +++++---
 t/t3200-branch.sh | 15 +++++++++++++++
 2 files changed, 20 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..ce478d85cb 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1080,6 +1080,21 @@ 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 &&
+	test_grep "git branch --set-upstream-to=hintremote/hintbranch\$" err &&
+	test_grep ! "refs/heads/" err
+'
+
 test_expect_success 'use --set-upstream-to modify HEAD' '
 	test_config branch.main.remote foo &&
 	test_config branch.main.merge foo &&

base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
-- 
gitgitgadget

             reply	other threads:[~2026-10-20  1:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  1:15 tsukasa-ino via GitGitGadget [this message]
2026-10-20 19:51 ` [PATCH] branch: strip refs/heads/ from the --set-upstream-to hint Junio C Hamano
2026-10-21  7:05 ` Harald Nordgren
2026-10-21 21:35 ` [PATCH v2 0/3] checkout: finish switch when tracking config cannot be written tsukasa-ino via GitGitGadget
2026-10-21 21:35   ` [PATCH v2 1/3] branch: strip refs/heads/ from the --set-upstream-to hint tsukasa inoue via GitGitGadget
2026-10-21 21:35   ` [PATCH v2 2/3] branch: return an error from setup_tracking() tsukasa inoue via GitGitGadget
2026-10-21 21:35   ` [PATCH v2 3/3] checkout: finish switching when the tracking config cannot be written tsukasa inoue via GitGitGadget
2026-10-21 22:20   ` [PATCH v2 0/3] checkout: finish switch when " Junio C Hamano
2026-10-22  2:17     ` 井上司

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2268.git.1792458904027.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=inoue.tsukasa.4@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox