{"thread":{"id":"66629","subject":"[PATCH 0/2] checkout: finish switch when tracking config cannot be written","startedAt":"2026-10-22T02:51:33Z","lastAt":"2026-10-23T18:30:49Z","messageCount":4,"participants":["tsukasa-ino via GitGitGadget","tsukasa inoue via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"555811","messageId":"pull.2275.git.1792637495.gitgitgadget@gmail.com","threadId":"66629","inReplyTo":null,"subject":"[PATCH 0/2] checkout: finish switch when tracking config cannot be written","fromName":"tsukasa-ino via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-22T02:51:33Z","receivedAt":"2026-10-22T02:51:33Z","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.\n\n * 1/2 makes setup_tracking() return an error instead of calling exit(). No\n   behavior change yet.\n * 2/2 makes switch and checkout finish the switch and exit with status 0,\n   as push -u, clone and fetch already do. git branch still fails.\n\nThese patches were first sent as part of v2 of a series that also fixed the\nhint\n(https://lore.kernel.org/git/pull.2268.v2.git.1792618518.gitgitgadget@gmail.com/).\nJunio asked whether they can be treated separately from the hint fix\n(https://lore.kernel.org/git/xmqq7bjalmpt.fsf@gitster.g/), and I replied\nthat they can\n(https://lore.kernel.org/git/CAP8tz2KOXHLjc5V1o1tZZWSsgOjVrp-ekH7H5KszBGK+hRqwrg@mail.gmail.com/).\nThe direction was agreed in the bug report thread\n(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 (2):\n  branch: return an error from setup_tracking()\n  checkout: finish switching when the tracking config cannot be written\n\n branch.c                  | 35 +++++++++++++++++++++--------------\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         | 13 +++++++++++++\n 7 files changed, 105 insertions(+), 27 deletions(-)\n\n\nbase-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2275%2FtsukasaI%2Ffix%2Fcheckout-tracking-config-failure-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2275/tsukasaI/fix/checkout-tracking-config-failure-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2275\n-- \ngitgitgadget\n\n"},{"id":"555812","messageId":"a9865116582860d7d5191af2dcca9e6d742008c4.1792637495.git.gitgitgadget@gmail.com","threadId":"66629","inReplyTo":"pull.2275.git.1792637495.gitgitgadget@gmail.com","subject":"[PATCH 1/2] branch: return an error from setup_tracking()","fromName":"tsukasa inoue via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-22T02:51:34Z","receivedAt":"2026-10-22T02:51:34Z","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 22f4f46b96..27be5ecc3e 100644\n--- a/branch.c\n+++ b/branch.c\n@@ -249,12 +249,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@@ -343,11 +344,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@@ -627,10 +629,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@@ -639,6 +641,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@@ -676,21 +679,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@@ -840,8 +847,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 2d575a4f57..13a2938b42 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":"555813","messageId":"fcc73a773a7eba2612d983f8957b82f3d4fc09ef.1792637495.git.gitgitgadget@gmail.com","threadId":"66629","inReplyTo":"pull.2275.git.1792637495.gitgitgadget@gmail.com","subject":"[PATCH 2/2] checkout: finish switching when the tracking config cannot be written","fromName":"tsukasa inoue via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-22T02:51:35Z","receivedAt":"2026-10-22T02:51:35Z","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 13a2938b42..c5e2202026 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 cdb6c6a634..ca1a1c7f86 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1080,6 +1080,19 @@ 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 '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":"556095","messageId":"xmqqmrs45kx2.fsf@gitster.g","threadId":"66629","inReplyTo":"pull.2275.git.1792637495.gitgitgadget@gmail.com","subject":"Re: [PATCH 0/2] checkout: finish switch when tracking config cannot be written","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-23T18:30:49Z","receivedAt":"2026-10-23T18:30:49Z","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.\n>\n>  * 1/2 makes setup_tracking() return an error instead of calling exit(). No\n>    behavior change yet.\n>  * 2/2 makes switch and checkout finish the switch and exit with status 0,\n>    as push -u, clone and fetch already do. git branch still fails.\n\nThanks, queued.\n\n"}]}