{"thread":{"id":"66597","subject":"[PATCH] branch: strip refs/heads/ from the --set-upstream-to hint","startedAt":"2026-10-20T01:15:04Z","lastAt":"2026-10-22T02:17:12Z","messageCount":9,"participants":["tsukasa-ino via GitGitGadget","Junio C Hamano","Harald Nordgren","tsukasa inoue via GitGitGadget","井上司"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"555486","messageId":"pull.2268.git.1792458904027.gitgitgadget@gmail.com","threadId":"66597","inReplyTo":null,"subject":"[PATCH] branch: strip refs/heads/ from the --set-upstream-to hint","fromName":"tsukasa-ino via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-20T01:15:04Z","receivedAt":"2026-10-20T01:15:04Z","isPatch":true,"sender":{"key":"name:tsukasa-ino","avatar":null},"body":"From: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n\nWhen git switch fails to write the tracking config, it prints a hint\nwith the command to set the upstream by hand. For example, if\n.git/config.lock exists:\n\n    error: could not lock config file .git/config: File exists\n    error: unable to write upstream branch configuration\n    hint:\n    hint: After fixing the error cause you may try to fix up\n    hint: the remote tracking information by invoking:\n    hint:   git branch --set-upstream-to=origin/refs/heads/feat\n\nThe hint says origin/refs/heads/feat, but the correct name is\norigin/feat. Running the command as printed fails, because the full\nref name is not a valid remote-tracking branch name here.\n\nThis has been wrong since a3f40ec4b0 (branch: accept multiple upstream\nbranches for tracking, 2021-12-20).\n\nStrip refs/heads/ from the hint, as the success message in the same\nfunction already does. The git config --add line for multiple upstreams\nstill needs the full ref name, so it is not changed.\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n    branch: strip refs/heads/ from the --set-upstream-to hint\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2268%2FtsukasaI%2Ffix%2Fbranch-upstream-hint-shortname-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2268\n\n branch.c          |  8 +++++---\n t/t3200-branch.sh | 15 +++++++++++++++\n 2 files changed, 20 insertions(+), 3 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 22f4f46b96..bb07846911 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -186,12 +186,14 @@ out_err:\n \n \tadvise(_(\"\\nAfter fixing the error cause you may try to fix up\\n\"\n \t\t\"the remote tracking information by invoking:\"));\n-\tif (remotes->nr == 1)\n+\tif (remotes->nr == 1) {\n+\t\tshortname = remotes->items[0].string;\n+\t\tskip_prefix(shortname, \"refs/heads/\", &shortname);\n \t\tadvise(\"  git branch --set-upstream-to=%s%s%s\",\n \t\t\torigin ? origin : \"\",\n \t\t\torigin ? \"/\" : \"\",\n-\t\t\tremotes->items[0].string);\n-\telse {\n+\t\t\tshortname);\n+\t} else {\n \t\tadvise(\"  git config --add branch.\\\"%s\\\".remote %s\",\n \t\t\tlocal, origin ? origin : \".\");\n \t\tfor_each_string_list_item(item, remotes)\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..ce478d85cb 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1080,6 +1080,21 @@ test_expect_success '--set-upstream-to fails on locked config' '\n \ttest_grep \"could not lock config file .git/config\" err\n '\n \n+test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '\n+\ttest_when_finished \"git branch -D hint-local\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/hintremote/hintbranch\" &&\n+\ttest_config remote.hintremote.url . &&\n+\ttest_config remote.hintremote.fetch \"+refs/heads/*:refs/remotes/hintremote/*\" &&\n+\tgit update-ref refs/remotes/hintremote/hintbranch HEAD &&\n+\tgit branch hint-local &&\n+\ttest_when_finished \"rm -f .git/config.lock\" &&\n+\t>.git/config.lock &&\n+\ttest_must_fail git -c core.configLockTimeout=0 \\\n+\t\tbranch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&\n+\ttest_grep \"git branch --set-upstream-to=hintremote/hintbranch\\$\" err &&\n+\ttest_grep ! \"refs/heads/\" err\n+'\n+\n test_expect_success 'use --set-upstream-to modify HEAD' '\n \ttest_config branch.main.remote foo &&\n \ttest_config branch.main.merge foo &&\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n-- \ngitgitgadget\n\n"},{"id":"555618","messageId":"xmqq7bjcup46.fsf@gitster.g","threadId":"66597","inReplyTo":"pull.2268.git.1792458904027.gitgitgadget@gmail.com","subject":"Re: [PATCH] branch: strip refs/heads/ from the --set-upstream-to hint","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-20T19:51:53Z","receivedAt":"2026-10-20T19:51:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"tsukasa-ino via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n>     hint:   git branch --set-upstream-to=origin/refs/heads/feat\n>\n> The hint says origin/refs/heads/feat, but the correct name is\n> origin/feat. Running the command as printed fails, because the full\n> ref name is not a valid remote-tracking branch name here.\n\nGood observation.  Thanks for noticing.\n\n> +test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '\n> +\ttest_when_finished \"git branch -D hint-local\" &&\n> +\ttest_when_finished \"git update-ref -d refs/remotes/hintremote/hintbranch\" &&\n> +\ttest_config remote.hintremote.url . &&\n> +\ttest_config remote.hintremote.fetch \"+refs/heads/*:refs/remotes/hintremote/*\" &&\n> +\tgit update-ref refs/remotes/hintremote/hintbranch HEAD &&\n> +\tgit branch hint-local &&\n> +\ttest_when_finished \"rm -f .git/config.lock\" &&\n> +\t>.git/config.lock &&\n> +\ttest_must_fail git -c core.configLockTimeout=0 \\\n> +\t\tbranch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&\n> +\ttest_grep \"git branch --set-upstream-to=hintremote/hintbranch\\$\" err &&\n> +\ttest_grep ! \"refs/heads/\" err\n\nPerhaps it makes sense to also test that the suggested command line\nis accepted after all of the above?\n\n> +'\n\nThanks.\n\n"},{"id":"555678","messageId":"CAHwyqnU-eQBPTht3eHJceyWMehtQ2GAyEyZ8ObmVru0Y9EVkdQ@mail.gmail.com","threadId":"66597","inReplyTo":"pull.2268.git.1792458904027.gitgitgadget@gmail.com","subject":"Re: [PATCH] branch: strip refs/heads/ from the --set-upstream-to hint","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-21T07:05:12Z","receivedAt":"2026-10-21T07:05:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> When git switch fails to write the tracking config, it prints a hint\n> with the command to set the upstream by hand. For example, if\n> .git/config.lock exists:\n>\n>     error: could not lock config file .git/config: File exists\n>     error: unable to write upstream branch configuration\n>     hint:\n>     hint: After fixing the error cause you may try to fix up\n>     hint: the remote tracking information by invoking:\n>     hint:   git branch --set-upstream-to=origin/refs/heads/feat\n>\n> The hint says origin/refs/heads/feat, but the correct name is\n> origin/feat. Running the command as printed fails, because the full\n> ref name is not a valid remote-tracking branch name here.\n\nVery nice find!\n\n> +test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '\n> +       test_when_finished \"git branch -D hint-local\" &&\n> +       test_when_finished \"git update-ref -d refs/remotes/hintremote/hintbranch\" &&\n> +       test_config remote.hintremote.url . &&\n> +       test_config remote.hintremote.fetch \"+refs/heads/*:refs/remotes/hintremote/*\" &&\n> +       git update-ref refs/remotes/hintremote/hintbranch HEAD &&\n> +       git branch hint-local &&\n> +       test_when_finished \"rm -f .git/config.lock\" &&\n> +       >.git/config.lock &&\n> +       test_must_fail git -c core.configLockTimeout=0 \\\n> +               branch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&\n> +       test_grep \"git branch --set-upstream-to=hintremote/hintbranch\\$\" err &&\n> +       test_grep ! \"refs/heads/\" err\n> +'\n\nWould be good to test the full multiline hint message. After all, it's\nthe point of the topic.\n\nRight now, you are asserting that the correct form\n'hintremote/hintbranch' works, but we want to make sure that the hint\nactually gives that.\n\n\nHarald\n\n"},{"id":"555797","messageId":"pull.2268.v2.git.1792618518.gitgitgadget@gmail.com","threadId":"66597","inReplyTo":"pull.2268.git.1792458904027.gitgitgadget@gmail.com","subject":"[PATCH v2 0/3] checkout: finish switch when tracking config cannot be written","fromName":"tsukasa-ino via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T21:35:15Z","receivedAt":"2026-10-21T21:35:15Z","isPatch":true,"sender":{"key":"name:tsukasa-ino","avatar":null},"body":"When the tracking config cannot be written (for example, because\n.git/config.lock exists), git switch -c and checkout -b stop halfway. The\nworktree, the index and the new branch ref are already updated, but HEAD\nstays on the old branch, and the changes are left staged. The hint printed\nat that point was also wrong: it suggested origin/refs/heads/.\n\n1/3 strips refs/heads/ from the hint. 2/3 makes setup_tracking() return an\nerror instead of calling exit(). No behavior change yet. 3/3 makes switch\nand checkout finish the switch and exit with status 0, as push -u, clone and\nfetch already do. git branch still fails.\n\nChanges since v1:\n\n * 1/3: the test now compares the whole hint message, as suggested by Harald\n   Nordgren. It also checks that the command in the hint works once the lock\n   is removed, as suggested by Junio\n   (https://lore.kernel.org/git/xmqq7bjcup46.fsf@gitster.g/).\n * 2/3 and 3/3 are new. They implement the direction agreed on the bug\n   report thread (https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).\n\nMaking the switch fully atomic, so that a failure undoes the worktree, the\nindex, the ref and the config, is left for a separate topic.\n\ntsukasa inoue (3):\n  branch: strip refs/heads/ from the --set-upstream-to hint\n  branch: return an error from setup_tracking()\n  checkout: finish switching when the tracking config cannot be written\n\n branch.c                  | 43 +++++++++++++++++++++++----------------\n branch.h                  | 21 +++++++++++--------\n builtin/branch.c          | 17 +++++++++++-----\n builtin/checkout.c        |  6 ++++++\n t/t2027-checkout-track.sh | 20 ++++++++++++++++++\n t/t2060-switch.sh         | 20 ++++++++++++++++++\n t/t3200-branch.sh         | 38 ++++++++++++++++++++++++++++++++++\n 7 files changed, 135 insertions(+), 30 deletions(-)\n\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2268%2FtsukasaI%2Ffix%2Fbranch-upstream-hint-shortname-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/2268\n\nRange-diff vs v1:\n\n 1:  14eac9118e ! 1:  e705918378 branch: strip refs/heads/ from the --set-upstream-to hint\n     @@ t/t3200-branch.sh: test_expect_success '--set-upstream-to fails on locked config\n      +\t>.git/config.lock &&\n      +\ttest_must_fail git -c core.configLockTimeout=0 \\\n      +\t\tbranch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&\n     -+\ttest_grep \"git branch --set-upstream-to=hintremote/hintbranch\\$\" err &&\n     -+\ttest_grep ! \"refs/heads/\" err\n     ++\tcat >expect <<-\\EOF &&\n     ++\thint:\n     ++\thint: After fixing the error cause you may try to fix up\n     ++\thint: the remote tracking information by invoking:\n     ++\thint:   git branch --set-upstream-to=hintremote/hintbranch\n     ++\tEOF\n     ++\tgrep \"^hint:\" err >actual &&\n     ++\ttest_cmp expect actual &&\n     ++\trm -f .git/config.lock &&\n     ++\tgit branch --set-upstream-to=hintremote/hintbranch hint-local &&\n     ++\ttest_cmp_config hintremote branch.hint-local.remote &&\n     ++\ttest_cmp_config refs/heads/hintbranch branch.hint-local.merge\n      +'\n      +\n       test_expect_success 'use --set-upstream-to modify HEAD' '\n -:  ---------- > 2:  4d2cbaf170 branch: return an error from setup_tracking()\n -:  ---------- > 3:  be94ac75bf checkout: finish switching when the tracking config cannot be written\n\n-- \ngitgitgadget\n\n"},{"id":"555798","messageId":"e7059183786a5983bdebda34b3eb8f8f822ca9c0.1792618518.git.gitgitgadget@gmail.com","threadId":"66597","inReplyTo":"pull.2268.v2.git.1792618518.gitgitgadget@gmail.com","subject":"[PATCH v2 1/3] branch: strip refs/heads/ from the --set-upstream-to hint","fromName":"tsukasa inoue via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T21:35:16Z","receivedAt":"2026-10-21T21:35:16Z","isPatch":true,"sender":{"key":"name:tsukasa inoue","avatar":null},"body":"From: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n\nWhen git switch fails to write the tracking config, it prints a hint\nwith the command to set the upstream by hand. For example, if\n.git/config.lock exists:\n\n    error: could not lock config file .git/config: File exists\n    error: unable to write upstream branch configuration\n    hint:\n    hint: After fixing the error cause you may try to fix up\n    hint: the remote tracking information by invoking:\n    hint:   git branch --set-upstream-to=origin/refs/heads/feat\n\nThe hint says origin/refs/heads/feat, but the correct name is\norigin/feat. Running the command as printed fails, because the full\nref name is not a valid remote-tracking branch name here.\n\nThis has been wrong since a3f40ec4b0 (branch: accept multiple upstream\nbranches for tracking, 2021-12-20).\n\nStrip refs/heads/ from the hint, as the success message in the same\nfunction already does. The git config --add line for multiple upstreams\nstill needs the full ref name, so it is not changed.\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n branch.c          |  8 +++++---\n t/t3200-branch.sh | 25 +++++++++++++++++++++++++\n 2 files changed, 30 insertions(+), 3 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex 22f4f46b96..bb07846911 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -186,12 +186,14 @@ out_err:\n \n \tadvise(_(\"\\nAfter fixing the error cause you may try to fix up\\n\"\n \t\t\"the remote tracking information by invoking:\"));\n-\tif (remotes->nr == 1)\n+\tif (remotes->nr == 1) {\n+\t\tshortname = remotes->items[0].string;\n+\t\tskip_prefix(shortname, \"refs/heads/\", &shortname);\n \t\tadvise(\"  git branch --set-upstream-to=%s%s%s\",\n \t\t\torigin ? origin : \"\",\n \t\t\torigin ? \"/\" : \"\",\n-\t\t\tremotes->items[0].string);\n-\telse {\n+\t\t\tshortname);\n+\t} else {\n \t\tadvise(\"  git config --add branch.\\\"%s\\\".remote %s\",\n \t\t\tlocal, origin ? origin : \".\");\n \t\tfor_each_string_list_item(item, remotes)\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex cdb6c6a634..ccfb53efa3 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1080,6 +1080,31 @@ test_expect_success '--set-upstream-to fails on locked config' '\n \ttest_grep \"could not lock config file .git/config\" err\n '\n \n+test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '\n+\ttest_when_finished \"git branch -D hint-local\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/hintremote/hintbranch\" &&\n+\ttest_config remote.hintremote.url . &&\n+\ttest_config remote.hintremote.fetch \"+refs/heads/*:refs/remotes/hintremote/*\" &&\n+\tgit update-ref refs/remotes/hintremote/hintbranch HEAD &&\n+\tgit branch hint-local &&\n+\ttest_when_finished \"rm -f .git/config.lock\" &&\n+\t>.git/config.lock &&\n+\ttest_must_fail git -c core.configLockTimeout=0 \\\n+\t\tbranch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&\n+\tcat >expect <<-\\EOF &&\n+\thint:\n+\thint: After fixing the error cause you may try to fix up\n+\thint: the remote tracking information by invoking:\n+\thint:   git branch --set-upstream-to=hintremote/hintbranch\n+\tEOF\n+\tgrep \"^hint:\" err >actual &&\n+\ttest_cmp expect actual &&\n+\trm -f .git/config.lock &&\n+\tgit branch --set-upstream-to=hintremote/hintbranch hint-local &&\n+\ttest_cmp_config hintremote branch.hint-local.remote &&\n+\ttest_cmp_config refs/heads/hintbranch branch.hint-local.merge\n+'\n+\n test_expect_success 'use --set-upstream-to modify HEAD' '\n \ttest_config branch.main.remote foo &&\n \ttest_config branch.main.merge foo &&\n-- \ngitgitgadget\n\n\n"},{"id":"555799","messageId":"4d2cbaf17032a60aac15c081303f13e1d8eb87e5.1792618518.git.gitgitgadget@gmail.com","threadId":"66597","inReplyTo":"pull.2268.v2.git.1792618518.gitgitgadget@gmail.com","subject":"[PATCH v2 2/3] branch: return an error from setup_tracking()","fromName":"tsukasa inoue via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T21:35:17Z","receivedAt":"2026-10-21T21:35:17Z","isPatch":true,"sender":{"key":"name:tsukasa inoue","avatar":null},"body":"From: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n\nsetup_tracking() calls exit(1) when it cannot write the tracking\nconfiguration. This exit was introduced in 27852b2c53 (branch: report\nerrors in tracking branch setup, 2016-02-22). Because of it, callers\ncannot continue after a tracking failure, even though the ref has\nalready been created.\n\nChange setup_tracking() to return an error instead of exiting, and make\ncreate_branch() and dwim_and_setup_tracking() return that value too.\n\nThis does not change behavior. Callers still exit with status 1 on\nfailure, as before. Changing checkout\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n branch.c           | 35 +++++++++++++++++++++--------------\n branch.h           | 21 +++++++++++++--------\n builtin/branch.c   | 17 ++++++++++++-----\n builtin/checkout.c | 17 +++++++++--------\n 4 files changed, 55 insertions(+), 35 deletions(-)\n\ndiff --git a/branch.c b/branch.c\nindex bb07846911..7876892cc5 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -251,12 +251,13 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)\n  * will not be expanded to \"refs/remotes/origin/main\", so it is not safe\n  * for 'orig_ref' to be raw user input.\n  */\n-static void setup_tracking(const char *new_ref, const char *orig_ref,\n-\t\t\t   enum branch_track track, int quiet)\n+static int setup_tracking(const char *new_ref, const char *orig_ref,\n+\t\t\t  enum branch_track track, int quiet)\n {\n \tstruct tracking tracking;\n \tstruct string_list tracking_srcs = STRING_LIST_INIT_DUP;\n \tint config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;\n+\tint ret = 0;\n \tstruct find_tracked_branch_cb ftb_cb = {\n \t\t.tracking = &tracking,\n \t\t.ambiguous_remotes = STRING_LIST_INIT_DUP,\n@@ -345,11 +346,12 @@ static void setup_tracking(const char *new_ref, const char *orig_ref,\n \t\tstring_list_append(tracking.srcs, orig_ref);\n \tif (install_branch_config_multiple_remotes(config_flags, new_ref,\n \t\t\t\ttracking.remote, tracking.srcs) < 0)\n-\t\texit(1);\n+\t\tret = -1;\n \n cleanup:\n \tstring_list_clear(&tracking_srcs, 0);\n \tstring_list_clear(&ftb_cb.ambiguous_remotes, 0);\n+\treturn ret;\n }\n \n int read_branch_desc(struct strbuf *buf, const char *branch_name)\n@@ -629,10 +631,10 @@ static void dwim_branch_start(struct repository *r, const char *start_name,\n \tFREE_AND_NULL(real_ref);\n }\n \n-void create_branch(struct repository *r,\n-\t\t   const char *name, const char *start_name,\n-\t\t   int force, int clobber_head_ok, int reflog,\n-\t\t   int quiet, enum branch_track track, int dry_run)\n+int create_branch(struct repository *r,\n+\t\t  const char *name, const char *start_name,\n+\t\t  int force, int clobber_head_ok, int reflog,\n+\t\t  int quiet, enum branch_track track, int dry_run)\n {\n \tstruct object_id oid;\n \tchar *real_ref;\n@@ -641,6 +643,7 @@ void create_branch(struct repository *r,\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \tint flags = 0;\n+\tint ret = 0;\n \tchar *msg;\n \n \tif (track == BRANCH_TRACK_OVERRIDE)\n@@ -678,21 +681,25 @@ void create_branch(struct repository *r,\n \tfree(msg);\n \n \tif (real_ref && track)\n-\t\tsetup_tracking(ref.buf + 11, real_ref, track, quiet);\n+\t\tret = setup_tracking(ref.buf + 11, real_ref, track, quiet);\n \n cleanup:\n \tstrbuf_release(&ref);\n \tfree(real_ref);\n+\treturn ret;\n }\n \n-void dwim_and_setup_tracking(struct repository *r, const char *new_ref,\n-\t\t\t     const char *orig_ref, enum branch_track track,\n-\t\t\t     int quiet)\n+int dwim_and_setup_tracking(struct repository *r, const char *new_ref,\n+\t\t\t    const char *orig_ref, enum branch_track track,\n+\t\t\t    int quiet)\n {\n \tchar *real_orig_ref = NULL;\n+\tint ret;\n+\n \tdwim_branch_start(r, orig_ref, track, &real_orig_ref, NULL);\n-\tsetup_tracking(new_ref, real_orig_ref, track, quiet);\n+\tret = setup_tracking(new_ref, real_orig_ref, track, quiet);\n \tfree(real_orig_ref);\n+\treturn ret;\n }\n \n /**\n@@ -842,8 +849,8 @@ void create_branches_recursively(struct repository *r, const char *name,\n \t * tedious to determine whether or not tracking was set up in the\n \t * superproject.\n \t */\n-\tif (track)\n-\t\tsetup_tracking(name, tracking_name, track, quiet);\n+\tif (track && setup_tracking(name, tracking_name, track, quiet))\n+\t\texit(1);\n \n \tfor (i = 0; i < submodule_entry_list.entry_nr; i++) {\n \t\tif (submodule_create_branch(\ndiff --git a/branch.h b/branch.h\nindex e9b1f7b37d..4bc4527284 100644\n--- a/branch.h\n+++ b/branch.h\n@@ -34,10 +34,13 @@ enum branch_track {\n  *     causes new_ref to track whatever orig_ref tracks.\n  *\n  *   - quiet suppresses tracking information.\n+ *\n+ * Returns 0 on success, or -1 if the tracking configuration could not be\n+ * written.\n  */\n-void dwim_and_setup_tracking(struct repository *r, const char *new_ref,\n-\t\t\t     const char *orig_ref, enum branch_track track,\n-\t\t\t     int quiet);\n+int dwim_and_setup_tracking(struct repository *r, const char *new_ref,\n+\t\t\t    const char *orig_ref, enum branch_track track,\n+\t\t\t    int quiet);\n \n /*\n  * Creates a new branch, where:\n@@ -63,12 +66,14 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,\n  *\n  *   - dry_run causes the branch to be validated but not created.\n  *\n+ * Returns 0 on success, or -1 if the tracking configuration could not be\n+ * written.\n  */\n-void create_branch(struct repository *r,\n-\t\t   const char *name, const char *start_name,\n-\t\t   int force, int clobber_head_ok,\n-\t\t   int reflog, int quiet, enum branch_track track,\n-\t\t   int dry_run);\n+int create_branch(struct repository *r,\n+\t\t  const char *name, const char *start_name,\n+\t\t  int force, int clobber_head_ok,\n+\t\t  int reflog, int quiet, enum branch_track track,\n+\t\t  int dry_run);\n \n /*\n  * Creates a new branch in a repository and its submodules (and its\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..b536b619bb 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -1253,9 +1253,13 @@ int cmd_branch(int argc,\n \t\t\tdie(_(\"branch '%s' does not exist\"), branch->name);\n \t\t}\n \n-\t\tdwim_and_setup_tracking(the_repository, branch->name,\n-\t\t\t\t\tnew_upstream, BRANCH_TRACK_OVERRIDE,\n-\t\t\t\t\tquiet);\n+\t\tif (dwim_and_setup_tracking(the_repository, branch->name,\n+\t\t\t\t\t    new_upstream, BRANCH_TRACK_OVERRIDE,\n+\t\t\t\t\t    quiet)) {\n+\t\t\tstrbuf_release(&buf);\n+\t\t\tret = 1;\n+\t\t\tgoto out;\n+\t\t}\n \t\tstrbuf_release(&buf);\n \t} else if (unset_upstream) {\n \t\tstruct branch *branch;\n@@ -1305,8 +1309,11 @@ int cmd_branch(int argc,\n \t\t\tret = 0;\n \t\t\tgoto out;\n \t\t}\n-\t\tcreate_branch(the_repository, branch_name, start_name, force, 0,\n-\t\t\t      reflog, quiet, track, 0);\n+\t\tif (create_branch(the_repository, branch_name, start_name,\n+\t\t\t\t  force, 0, reflog, quiet, track, 0)) {\n+\t\t\tret = 1;\n+\t\t\tgoto out;\n+\t\t}\n \t} else\n \t\tusage_with_options(builtin_branch_usage, options);\n \ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex c0f0d2c700..87e067b45b 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -979,14 +979,15 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \t\t\tfree(refname);\n \t\t}\n \t\telse\n-\t\t\tcreate_branch(the_repository,\n-\t\t\t\t      opts->new_branch, new_branch_info->name,\n-\t\t\t\t      opts->new_branch_force ? 1 : 0,\n-\t\t\t\t      opts->new_branch_force ? 1 : 0,\n-\t\t\t\t      opts->new_branch_log,\n-\t\t\t\t      opts->quiet,\n-\t\t\t\t      opts->track,\n-\t\t\t\t      0);\n+\t\t\tif (create_branch(the_repository,\n+\t\t\t\t\t  opts->new_branch, new_branch_info->name,\n+\t\t\t\t\t  opts->new_branch_force ? 1 : 0,\n+\t\t\t\t\t  opts->new_branch_force ? 1 : 0,\n+\t\t\t\t\t  opts->new_branch_log,\n+\t\t\t\t\t  opts->quiet,\n+\t\t\t\t\t  opts->track,\n+\t\t\t\t\t  0))\n+\t\t\t\texit(1);\n \t\tfree(new_branch_info->name);\n \t\tfree(new_branch_info->refname);\n \t\tnew_branch_info->name = xstrdup(opts->new_branch);\n-- \ngitgitgadget\n\n\n"},{"id":"555800","messageId":"be94ac75bfbcb4ea6af982f5a5cffd20344445b5.1792618518.git.gitgitgadget@gmail.com","threadId":"66597","inReplyTo":"pull.2268.v2.git.1792618518.gitgitgadget@gmail.com","subject":"[PATCH v2 3/3] checkout: finish switching when the tracking config cannot be written","fromName":"tsukasa inoue via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-21T21:35:18Z","receivedAt":"2026-10-21T21:35:18Z","isPatch":true,"sender":{"key":"name:tsukasa inoue","avatar":null},"body":"From: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n\nWhen git switch -c <new> <remote>/<branch> (or checkout -b) cannot write\nthe tracking config, for example because .git/config.lock exists, the\nworktree, the index and the new branch ref have already been updated.\nGit then exits with status 1 while HEAD still points to the original\nbranch. The changes of the switch remain staged, and the post-checkout\nhook does not run.\n\ncreate_branch() now returns an error instead of exiting. Make checkout\nignore that error so that the switch completes and the command exits\nwith status 0. The error and the hint are still printed.\n\nMatch push -u, clone and fetch, which also print the error and continue.\nThe upstream can be set later with the command shown in the hint.\n\nKeep git branch <new> <start> failing when the tracking config cannot be\nwritten, so its behavior does not change; t3200 covers this. Making the\nswitch fully atomic, undoing the worktree, the index, the ref and the\nconfig when the tracking config fails, is a separate topic.\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n builtin/checkout.c        | 23 ++++++++++++++---------\n t/t2027-checkout-track.sh | 20 ++++++++++++++++++++\n t/t2060-switch.sh         | 20 ++++++++++++++++++++\n t/t3200-branch.sh         | 13 +++++++++++++\n 4 files changed, 67 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 87e067b45b..a203f9e2ca 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -979,15 +979,20 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \t\t\tfree(refname);\n \t\t}\n \t\telse\n-\t\t\tif (create_branch(the_repository,\n-\t\t\t\t\t  opts->new_branch, new_branch_info->name,\n-\t\t\t\t\t  opts->new_branch_force ? 1 : 0,\n-\t\t\t\t\t  opts->new_branch_force ? 1 : 0,\n-\t\t\t\t\t  opts->new_branch_log,\n-\t\t\t\t\t  opts->quiet,\n-\t\t\t\t\t  opts->track,\n-\t\t\t\t\t  0))\n-\t\t\t\texit(1);\n+\t\t\t/*\n+\t\t\t * Not fatal: the error and hint have already been\n+\t\t\t * shown, and like \"push -u\" and \"clone\" we finish\n+\t\t\t * the switch instead of leaving HEAD on the old\n+\t\t\t * branch with the worktree already updated.\n+\t\t\t */\n+\t\t\tcreate_branch(the_repository,\n+\t\t\t\t      opts->new_branch, new_branch_info->name,\n+\t\t\t\t      opts->new_branch_force ? 1 : 0,\n+\t\t\t\t      opts->new_branch_force ? 1 : 0,\n+\t\t\t\t      opts->new_branch_log,\n+\t\t\t\t      opts->quiet,\n+\t\t\t\t      opts->track,\n+\t\t\t\t      0);\n \t\tfree(new_branch_info->name);\n \t\tfree(new_branch_info->refname);\n \t\tnew_branch_info->name = xstrdup(opts->new_branch);\ndiff --git a/t/t2027-checkout-track.sh b/t/t2027-checkout-track.sh\nindex c01f1cd617..c3be183bfc 100755\n--- a/t/t2027-checkout-track.sh\n+++ b/t/t2027-checkout-track.sh\n@@ -47,6 +47,26 @@ test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' '\n \ttest_cmp_config refs/heads/main branch.b4.merge\n '\n \n+test_expect_success 'checkout -b completes when tracking config cannot be written' '\n+\ttest_when_finished \"git update-ref -d refs/remotes/lockremote/lockbranch\" &&\n+\ttest_when_finished \"git branch -D lock-new\" &&\n+\ttest_when_finished \"git checkout main\" &&\n+\ttest_config remote.lockremote.url . &&\n+\ttest_config remote.lockremote.fetch \"+refs/heads/*:refs/remotes/lockremote/*\" &&\n+\tgit update-ref refs/remotes/lockremote/lockbranch one &&\n+\ttest_when_finished \"rm -f .git/config.lock\" &&\n+\t>.git/config.lock &&\n+\tgit -c core.configLockTimeout=0 \\\n+\t\tcheckout -b lock-new lockremote/lockbranch 2>err &&\n+\ttest_grep \"unable to write upstream branch configuration\" err &&\n+\techo refs/heads/lock-new >expect &&\n+\tgit symbolic-ref HEAD >actual &&\n+\ttest_cmp expect actual &&\n+\tgit diff --cached --exit-code &&\n+\trm -f .git/config.lock &&\n+\ttest_must_fail git config branch.lock-new.remote\n+'\n+\n test_expect_success 'ambiguous tracking info' '\n \t# Set up a few remote repositories\n \tgit init --bare --initial-branch=trunk src1 &&\ndiff --git a/t/t2060-switch.sh b/t/t2060-switch.sh\nindex cec2fe6876..b4f137acc3 100755\n--- a/t/t2060-switch.sh\n+++ b/t/t2060-switch.sh\n@@ -146,6 +146,26 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '\n \ttest_cmp_config \"\" --default \"\" branch.main2.merge\n '\n \n+test_expect_success 'switch -c completes when tracking config cannot be written' '\n+\ttest_when_finished \"git update-ref -d refs/remotes/lockremote/lockbranch\" &&\n+\ttest_when_finished \"git branch -D lock-new\" &&\n+\ttest_when_finished \"git switch main\" &&\n+\ttest_config remote.lockremote.url . &&\n+\ttest_config remote.lockremote.fetch \"+refs/heads/*:refs/remotes/lockremote/*\" &&\n+\tgit update-ref refs/remotes/lockremote/lockbranch first-branch &&\n+\ttest_when_finished \"rm -f .git/config.lock\" &&\n+\t>.git/config.lock &&\n+\tgit -c core.configLockTimeout=0 \\\n+\t\tswitch -c lock-new lockremote/lockbranch 2>err &&\n+\ttest_grep \"unable to write upstream branch configuration\" err &&\n+\techo refs/heads/lock-new >expect &&\n+\tgit symbolic-ref HEAD >actual &&\n+\ttest_cmp expect actual &&\n+\tgit diff --cached --exit-code &&\n+\trm -f .git/config.lock &&\n+\ttest_must_fail git config branch.lock-new.remote\n+'\n+\n test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '\n \ttest_when_finished \"\n \t\tgit worktree remove wt1 ||:\ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex ccfb53efa3..b9c169a91a 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1105,6 +1105,19 @@ test_expect_success '--set-upstream-to hint uses short remote-tracking branch na\n \ttest_cmp_config refs/heads/hintbranch branch.hint-local.merge\n '\n \n+test_expect_success 'branch with start point fails when tracking config cannot be written' '\n+\ttest_when_finished \"git branch -D lock-new || :\" &&\n+\ttest_when_finished \"git update-ref -d refs/remotes/lockremote/lockbranch\" &&\n+\ttest_config remote.lockremote.url . &&\n+\ttest_config remote.lockremote.fetch \"+refs/heads/*:refs/remotes/lockremote/*\" &&\n+\tgit update-ref refs/remotes/lockremote/lockbranch HEAD &&\n+\ttest_when_finished \"rm -f .git/config.lock\" &&\n+\t>.git/config.lock &&\n+\ttest_must_fail git -c core.configLockTimeout=0 \\\n+\t\tbranch lock-new lockremote/lockbranch 2>err &&\n+\ttest_grep \"unable to write upstream branch configuration\" err\n+'\n+\n test_expect_success 'use --set-upstream-to modify HEAD' '\n \ttest_config branch.main.remote foo &&\n \ttest_config branch.main.merge foo &&\n-- \ngitgitgadget\n\n"},{"id":"555805","messageId":"xmqq7bjalmpt.fsf@gitster.g","threadId":"66597","inReplyTo":"pull.2268.v2.git.1792618518.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 0/3] checkout: finish switch when tracking config cannot be written","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-21T22:20:46Z","receivedAt":"2026-10-21T22:20:46Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"tsukasa-ino via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> When the tracking config cannot be written (for example, because\n> .git/config.lock exists), git switch -c and checkout -b stop halfway. The\n> worktree, the index and the new branch ref are already updated, but HEAD\n> stays on the old branch, and the changes are left staged. The hint printed\n> at that point was also wrong: it suggested origin/refs/heads/.\n>\n> 1/3 strips refs/heads/ from the hint. 2/3 makes setup_tracking() return an\n> error instead of calling exit(). No behavior change yet. 3/3 makes switch\n> and checkout finish the switch and exit with status 0, as push -u, clone and\n> fetch already do. git branch still fails.\n>\n> Changes since v1:\n>\n>  * 1/3: the test now compares the whole hint message, as suggested by Harald\n>    Nordgren. It also checks that the command in the hint works once the lock\n>    is removed, as suggested by Junio\n>    (https://lore.kernel.org/git/xmqq7bjcup46.fsf@gitster.g/).\n\nOK.\n\n>  * 2/3 and 3/3 are new. They implement the direction agreed on the bug\n>    report thread (https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).\n\nI am not sure if these two should be part of the same topic as the\nfirst one, or they should be treated separately.  IOW, if the way\nthese two patches implement the fix proves to be contentious, does\nit make sense to hold the change in 1/3 until 2&3/3 are resolved\n(and vice versa), or can they be treated as two separate issues?\n\n> Making the switch fully atomic, so that a failure undoes the worktree, the\n> index, the ref and the config, is left for a separate topic.\n\nSensible.\n\n"},{"id":"555810","messageId":"CAP8tz2KOXHLjc5V1o1tZZWSsgOjVrp-ekH7H5KszBGK+hRqwrg@mail.gmail.com","threadId":"66597","inReplyTo":"xmqq7bjalmpt.fsf@gitster.g","subject":"Re: [PATCH v2 0/3] checkout: finish switch when tracking config cannot be written","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-10-22T02:17:12Z","receivedAt":"2026-10-22T02:17:12Z","isPatch":true,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"> I am not sure if these two should be part of the same topic as the\n> first one, or they should be treated separately.  IOW, if the way\n> these two patches implement the fix proves to be contentious, does\n> it make sense to hold the change in 1/3 until 2&3/3 are resolved\n> (and vice versa), or can they be treated as two separate issues?\n\nThanks for the question. I checked whether the patches can be split.\n\n- 2/3 applies without 1/3.\n- 3/3 conflicts with 1/3 only in t3200, because its new test is placed\n  right after the one added by 1/3. The code does not depend on 1/3. The\n  hint sentence in the 3/3 commit message assumes the corrected hint, but\n  that is only a wording dependency.\n\nSo 1/3 can be taken on its own. 2/3 and 3/3 can be discussed separately,\nand changes to them do not need to hold up 1/3. I will resend 2/3 and 3/3\nas a separate series on top of the current master, so that 1/3 can be\napplied first.\n\n2026年10月22日(木) 7:20 Junio C Hamano <gitster@pobox.com>:\n>\n> \"tsukasa-ino via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > When the tracking config cannot be written (for example, because\n> > .git/config.lock exists), git switch -c and checkout -b stop halfway. The\n> > worktree, the index and the new branch ref are already updated, but HEAD\n> > stays on the old branch, and the changes are left staged. The hint printed\n> > at that point was also wrong: it suggested origin/refs/heads/.\n> >\n> > 1/3 strips refs/heads/ from the hint. 2/3 makes setup_tracking() return an\n> > error instead of calling exit(). No behavior change yet. 3/3 makes switch\n> > and checkout finish the switch and exit with status 0, as push -u, clone and\n> > fetch already do. git branch still fails.\n> >\n> > Changes since v1:\n> >\n> >  * 1/3: the test now compares the whole hint message, as suggested by Harald\n> >    Nordgren. It also checks that the command in the hint works once the lock\n> >    is removed, as suggested by Junio\n> >    (https://lore.kernel.org/git/xmqq7bjcup46.fsf@gitster.g/).\n>\n> OK.\n>\n> >  * 2/3 and 3/3 are new. They implement the direction agreed on the bug\n> >    report thread (https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).\n>\n> I am not sure if these two should be part of the same topic as the\n> first one, or they should be treated separately.  IOW, if the way\n> these two patches implement the fix proves to be contentious, does\n> it make sense to hold the change in 1/3 until 2&3/3 are resolved\n> (and vice versa), or can they be treated as two separate issues?\n>\n> > Making the switch fully atomic, so that a failure undoes the worktree, the\n> > index, the ref and the config, is left for a separate topic.\n>\n> Sensible.\n\n"}]}