{"thread":{"id":"66598","subject":"switch/checkout stops halfway when it fails to write tracking config","startedAt":"2026-10-20T01:39:14Z","lastAt":"2026-11-08T12:54:03Z","messageCount":9,"participants":["井上司","Junio C Hamano","D. Ben Knoble"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"555489","messageId":"CAP8tz2JPXzq=Skokn4OtH8t6c3b+p1Wny1iX6AhXReUGvsBhuw@mail.gmail.com","threadId":"66598","inReplyTo":null,"subject":"switch/checkout stops halfway when it fails to write tracking config","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-10-20T01:39:14Z","receivedAt":"2026-10-20T01:39:14Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"1. What happened\nWhen writing the tracking config fails (e.g. .git/config.lock exists) while\nswitch or checkout creates a new branch, the command exits with code 1 in the\nmiddle of the switch. The worktree and index are already updated for the new\nbranch, but HEAD still points to the old branch. The user then sees unrelated\nstaged changes.\n\n2. Reproduction\ngit init -q -b main up\ncd up\necho one >file && git add file && git commit -q -m one\ngit branch feat\ncd ..\ngit clone -q up work\ncd up\ngit switch -q feat\necho two >>file && git commit -q -a -m two\ncd ../work\ngit fetch -q\ntouch .git/config.lock\ngit switch feat; echo \"exit=$?\"\ngit symbolic-ref HEAD\ngit status --short\n\nOutput (git 2.43.0):\nerror: could not lock config file .git/config: File exists\nerror: unable to write upstream branch configuration\nhint:\nhint: After fixing the error cause you may try to fix up\nhint: the remote tracking information by invoking:\nhint:   git branch --set-upstream-to=origin/refs/heads/feat\nexit=1\nrefs/heads/main\nM  file\n\n3. Expectation\nI expected it to either complete the switch, or fail without changing anything.\n\n4. Scope\nThe reproduction above uses DWIM (git switch feat). The same happens with\nswitch -c, checkout -b, and --track. With -C on another existing branch, that\nbranch is reset before the failure, so its old commits can only be found in\nthe reflog.\n\n5. Root cause\nupdate_refs_for_switch() -> create_branch() -> setup_tracking() fails to write\nthe config, and the code calls exit(1) in setup_tracking() (branch.c). At that\npoint the worktree and the branch have been changed, but HEAD has not. This\nexit was introduced in 27852b2c53 (branch: report errors in tracking branch\nsetup, 2016-02-22).\n\n6. Related\nThe wrong hint printed for the same failure is fixed separately in\n<lore URL>. That patch does not fix this problem.\n\n7. Next steps\nI would like to work on a fix. Should checkout finish the switch and only\nreport the tracking error, or should it fail before touching the worktree?\n\n"},{"id":"555492","messageId":"xmqq8q3typsg.fsf@gitster.g","threadId":"66598","inReplyTo":"CAP8tz2JPXzq=Skokn4OtH8t6c3b+p1Wny1iX6AhXReUGvsBhuw@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-20T04:11:27Z","receivedAt":"2026-10-20T04:11:27Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"井上司 <inoue.tsukasa.4@gmail.com> writes:\n\n> 7. Next steps\n> I would like to work on a fix. Should checkout finish the switch and only\n> report the tracking error, or should it fail before touching the worktree?\n\nEither would be an improvement over the reported behaviour, but if\nwe can make it atomic \"all-or-none\" matter, that would be more\npreferrable than switching without setting up the tracking.\n\n"},{"id":"555500","messageId":"CAP8tz2JNxfOZC1W4gvGSJ-=AzY7iBADj_KtzWb4sawyqjQ0hHw@mail.gmail.com","threadId":"66598","inReplyTo":"xmqq8q3typsg.fsf@gitster.g","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-10-20T06:33:51Z","receivedAt":"2026-10-20T06:33:51Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"Thanks for the reply.\n\nI agree with your point. However, implementing full atomicity now would\nbe a large change, for two reasons:\n\n- push -u, clone and fetch continue even when writing the tracking config\n  fails, but setup_tracking() calls exit() and aborts the process.\n- For full atomicity, we would need to undo the worktree, the ref and the\n  config. We would also need to handle the die() calls in checkout.\n\nSo I propose to make setup_tracking() return an error instead of calling\nexit(), so that switch and checkout can behave like the other callers,\nand to leave full atomicity as a separate topic.\n\n2026年10月20日(火) 13:11 Junio C Hamano <gitster@pobox.com>:\n>\n> 井上司 <inoue.tsukasa.4@gmail.com> writes:\n>\n> > 7. Next steps\n> > I would like to work on a fix. Should checkout finish the switch and only\n> > report the tracking error, or should it fail before touching the worktree?\n>\n> Either would be an improvement over the reported behaviour, but if\n> we can make it atomic \"all-or-none\" matter, that would be more\n> preferrable than switching without setting up the tracking.\n\n"},{"id":"555531","messageId":"CALnO6CCFfd3VcZkFGmc6kYHqEwsafa7UbOg-6cN0jEfsipQm5Q@mail.gmail.com","threadId":"66598","inReplyTo":"CAP8tz2JPXzq=Skokn4OtH8t6c3b+p1Wny1iX6AhXReUGvsBhuw@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-20T13:06:54Z","receivedAt":"2026-10-20T13:06:54Z","isPatch":false,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Mon, Oct 19, 2026 at 9:41 PM 井上司 <inoue.tsukasa.4@gmail.com> wrote:\n>\n> 5. Root cause\n> update_refs_for_switch() -> create_branch() -> setup_tracking() fails to write\n> the config, and the code calls exit(1) in setup_tracking() (branch.c). At that\n> point the worktree and the branch have been changed, but HEAD has not. This\n> exit was introduced in 27852b2c53 (branch: report errors in tracking branch\n> setup, 2016-02-22).\n\nI can see where create_branch() is called prior to setting up HEAD,\nbut I'm mildly confused about the index/wt updates prior to either! It\nseems to me that should be one of the last steps, hm. Unfortunately,\nI'm having a hard time finding the responsible code outside of the\n\"merge\" case (which probably has a similar issue).\n\n-- \nD. Ben Knoble\n\n"},{"id":"555557","messageId":"CAP8tz2+CQcQyTdj3YFpEaiVh=hVoK5Xo8WFxGZNMJoCCM-u=hQ@mail.gmail.com","threadId":"66598","inReplyTo":"CALnO6CCFfd3VcZkFGmc6kYHqEwsafa7UbOg-6cN0jEfsipQm5Q@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-10-20T15:08:51Z","receivedAt":"2026-10-20T15:08:51Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"> I can see where create_branch() is called prior to setting up HEAD,\n> but I'm mildly confused about the index/wt updates prior to either! It\n> seems to me that should be one of the last steps, hm. Unfortunately,\n> I'm having a hard time finding the responsible code outside of the\n> \"merge\" case (which probably has a similar issue).\n\n\n- merge_working_tree() updates the worktree and the index. It is called\n  from switch_branches() on every branch switch, not only with -m,\n  because do_merge starts at 1.\n- The \"merge\" here is not -m. It is the 2-way merge in unpack_trees().\n  -m only adds an autostash step for when that 2-way merge fails.\n- The order is: update the worktree, create the ref, write the tracking\n  config (which fails), then update HEAD. As you noticed, the worktree\n  and index are updated before the ref and HEAD, so a failure later\n  leaves those changes in place.\n- As you guessed, the -m case has the same problem. I tried\n  \"git switch -m -c y origin/main\" with the lock present (v2.56.0 plus\n  my hint patch, which only changes the hint text). It stops in the same\n  place. If local changes conflict, HEAD stays on main, the file is left\n  conflicted, and the changes remain in the stash.\n- Running the same command again in that conflicted state stops with\n  \"a branch named 'y' already exists\". Switching to the existing branch\n  y stops with \"you need to resolve your current index first\"\n  (checkout.c:887 in v2.56.0). Neither run changes the state.\n\n2026年10月20日(火) 22:07 D. Ben Knoble <ben.knoble@gmail.com>:\n>\n> On Mon, Oct 19, 2026 at 9:41 PM 井上司 <inoue.tsukasa.4@gmail.com> wrote:\n> >\n> > 5. Root cause\n> > update_refs_for_switch() -> create_branch() -> setup_tracking() fails to write\n> > the config, and the code calls exit(1) in setup_tracking() (branch.c). At that\n> > point the worktree and the branch have been changed, but HEAD has not. This\n> > exit was introduced in 27852b2c53 (branch: report errors in tracking branch\n> > setup, 2016-02-22).\n>\n> I can see where create_branch() is called prior to setting up HEAD,\n> but I'm mildly confused about the index/wt updates prior to either! It\n> seems to me that should be one of the last steps, hm. Unfortunately,\n> I'm having a hard time finding the responsible code outside of the\n> \"merge\" case (which probably has a similar issue).\n>\n> --\n> D. Ben Knoble\n\n"},{"id":"555643","messageId":"xmqqecdjsryk.fsf@gitster.g","threadId":"66598","inReplyTo":"CAP8tz2JNxfOZC1W4gvGSJ-=AzY7iBADj_KtzWb4sawyqjQ0hHw@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-21T02:33:23Z","receivedAt":"2026-10-21T02:33:23Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"井上司 <inoue.tsukasa.4@gmail.com> writes:\n\n> Thanks for the reply.\n>\n> I agree with your point. However, implementing full atomicity now would\n> be a large change, for two reasons:\n>\n> - push -u, clone and fetch continue even when writing the tracking config\n>   fails, but setup_tracking() calls exit() and aborts the process.\n> - For full atomicity, we would need to undo the worktree, the ref and the\n>   config. We would also need to handle the die() calls in checkout.\n>\n> So I propose to make setup_tracking() return an error instead of calling\n> exit(), so that switch and checkout can behave like the other callers,\n> and to leave full atomicity as a separate topic.\n\nSounds like a good direction to go.\n\n"},{"id":"555862","messageId":"CALnO6CB=tmTrNKcWOanb79Bm9gPT5H_dntNGRJGp6iEYHe_aBg@mail.gmail.com","threadId":"66598","inReplyTo":"CAP8tz2+CQcQyTdj3YFpEaiVh=hVoK5Xo8WFxGZNMJoCCM-u=hQ@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-22T12:48:19Z","receivedAt":"2026-10-22T12:48:19Z","isPatch":false,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Tue, Oct 20, 2026 at 11:09 AM 井上司 <inoue.tsukasa.4@gmail.com> wrote:\n>\n> > I can see where create_branch() is called prior to setting up HEAD,\n> > but I'm mildly confused about the index/wt updates prior to either! It\n> > seems to me that should be one of the last steps, hm. Unfortunately,\n> > I'm having a hard time finding the responsible code outside of the\n> > \"merge\" case (which probably has a similar issue).\n>\n>\n> - merge_working_tree() updates the worktree and the index. It is called\n>   from switch_branches() on every branch switch, not only with -m,\n>   because do_merge starts at 1.\n\nAh, thanks, I think that's what I overlooked. I wonder if it's a royal\npain to make that step and the index step come later in the sequence?\nBut that is more of a band-aid than a proper atomicity fix.\n\n> - The \"merge\" here is not -m. It is the 2-way merge in unpack_trees().\n>   -m only adds an autostash step for when that 2-way merge fails.\n> - The order is: update the worktree, create the ref, write the tracking\n>   config (which fails), then update HEAD. As you noticed, the worktree\n>   and index are updated before the ref and HEAD, so a failure later\n>   leaves those changes in place.\n> - As you guessed, the -m case has the same problem. I tried\n>   \"git switch -m -c y origin/main\" with the lock present (v2.56.0 plus\n>   my hint patch, which only changes the hint text). It stops in the same\n>   place. If local changes conflict, HEAD stays on main, the file is left\n>   conflicted, and the changes remain in the stash.\n> - Running the same command again in that conflicted state stops with\n>   \"a branch named 'y' already exists\". Switching to the existing branch\n>   y stops with \"you need to resolve your current index first\"\n>   (checkout.c:887 in v2.56.0). Neither run changes the state.\n\n[trimmed repeated quoted content]\n\n-- \nD. Ben Knoble\n\n"},{"id":"555938","messageId":"CAP8tz2+cJ4E+Nv_dHuY-8-DoGj7_nP1=98Nn5+M3qMb5WezYVw@mail.gmail.com","threadId":"66598","inReplyTo":"CALnO6CB=tmTrNKcWOanb79Bm9gPT5H_dntNGRJGp6iEYHe_aBg@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-10-22T23:48:47Z","receivedAt":"2026-10-22T23:48:47Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"> I wonder if it's a royal pain to make that step and the index step come\n> later in the sequence? But that is more of a band-aid than a proper\n> atomicity fix.\n\nI checked this against the code and tried two cases. I used a build of the\nseries (2.56.0.217.gfcc73a773a, master plus the series at\nhttps://lore.kernel.org/git/pull.2275.git.1792637495.gitgitgadget@gmail.com/).\n\nWhy the worktree update comes first: switch_branches() calls\nmerge_working_tree() before update_refs_for_switch(). If\nmerge_working_tree() fails, the function returns before the ref is created\nor HEAD is updated.\n\nCase A: a local change to f conflicts with the switch, and I ran\n\"git switch -c y origin/main\". It fails with \"Your local changes to the\nfollowing files would be overwritten by checkout\", exit 1. HEAD stays on\nmain, the local change is kept, no ref y is created, and branch.y.remote\nis not set. Nothing is left behind.\n\nIf the ref and the tracking config were written first, this failure would\nleave a new branch and its config behind. Moving the worktree update later\nwould move that cleanup into this case, so I agree that moving the step is\na band-aid rather than atomicity.\n\nSome failures can still happen after the worktree update:\n\nCase B: with .git/refs/heads/y.lock present, \"git switch -c y origin/main\"\nfails with \"fatal: cannot lock ref 'refs/heads/y'\", exit 128. HEAD stays on\nmain, and the switch's change to f stays staged. This is the same kind of\nhalfway state. Updating HEAD can also fail: checkout.c:1027-1028 calls\ndie(_(\"unable to update HEAD\")).\n\nThe tracking-config failure is handled by the series linked above: the\nswitch finishes and exits 0. Full atomicity, which would also undo the\nworktree and the ref when a later step fails, is left for a separate topic.\n\n2026年10月22日(木) 21:48 D. Ben Knoble <ben.knoble@gmail.com>:\n>\n> On Tue, Oct 20, 2026 at 11:09 AM 井上司 <inoue.tsukasa.4@gmail.com> wrote:\n> >\n> > > I can see where create_branch() is called prior to setting up HEAD,\n> > > but I'm mildly confused about the index/wt updates prior to either! It\n> > > seems to me that should be one of the last steps, hm. Unfortunately,\n> > > I'm having a hard time finding the responsible code outside of the\n> > > \"merge\" case (which probably has a similar issue).\n> >\n> >\n> > - merge_working_tree() updates the worktree and the index. It is called\n> >   from switch_branches() on every branch switch, not only with -m,\n> >   because do_merge starts at 1.\n>\n> Ah, thanks, I think that's what I overlooked. I wonder if it's a royal\n> pain to make that step and the index step come later in the sequence?\n> But that is more of a band-aid than a proper atomicity fix.\n>\n> > - The \"merge\" here is not -m. It is the 2-way merge in unpack_trees().\n> >   -m only adds an autostash step for when that 2-way merge fails.\n> > - The order is: update the worktree, create the ref, write the tracking\n> >   config (which fails), then update HEAD. As you noticed, the worktree\n> >   and index are updated before the ref and HEAD, so a failure later\n> >   leaves those changes in place.\n> > - As you guessed, the -m case has the same problem. I tried\n> >   \"git switch -m -c y origin/main\" with the lock present (v2.56.0 plus\n> >   my hint patch, which only changes the hint text). It stops in the same\n> >   place. If local changes conflict, HEAD stays on main, the file is left\n> >   conflicted, and the changes remain in the stash.\n> > - Running the same command again in that conflicted state stops with\n> >   \"a branch named 'y' already exists\". Switching to the existing branch\n> >   y stops with \"you need to resolve your current index first\"\n> >   (checkout.c:887 in v2.56.0). Neither run changes the state.\n>\n> [trimmed repeated quoted content]\n>\n> --\n> D. Ben Knoble\n\n"},{"id":"557842","messageId":"CAP8tz2K2u4yuNE5ck777JVAgCk0-4yoToc1RNaFwMDKVH4zabA@mail.gmail.com","threadId":"66598","inReplyTo":"CAP8tz2+cJ4E+Nv_dHuY-8-DoGj7_nP1=98Nn5+M3qMb5WezYVw@mail.gmail.com","subject":"Re: switch/checkout stops halfway when it fails to write tracking config","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-08T12:54:03Z","receivedAt":"2026-11-08T12:54:03Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"To: Junio C Hamano <gitster@pobox.com>\nCc: git@vger.kernel.org, \"D. Ben Knoble\" <ben.knoble@gmail.com>,\nPatrick Steinhardt <ps@pks.im>\nSubject: Re: switch/checkout stops halfway when it fails to write\ntracking config\nIn-Reply-To: <xmqqecdjsryk.fsf@gitster.g>\n\nJunio C Hamano <gitster@pobox.com> writes:\n\n> > ... and to leave full atomicity as a separate topic.\n>\n> Sounds like a good direction to go.\n\nThanks for merging ti/checkout-track-failure, and thanks to Ben for the\nquestions in this thread.\n\nI'd like to work on the all-or-none switch now. Today switch_branches()\nupdates the worktree first, then creates the branch ref, writes the\ntracking config and updates HEAD, so a failure after the first step\nleaves the worktree switched. I looked at two ways to fix this:\n\nA. Put the branch ref and HEAD in one transaction, prepare it before\n   merge_working_tree(), and commit or abort it afterwards.\n\nB. Prepare that transaction only as a check and abort it, then do the\n   real updates after the worktree update, as today.\n\nI tried both with \"git update-ref --stdin\" on current master, files and\nreftable:\n\n - In both, a stale branch lock or a reference-transaction hook rejecting\n   at \"prepared\" stops the switch before the worktree changes.\n - With A, every other ref update in a reftable repository fails while\n   the transaction is held, and with files a commit from another process\n   fails on HEAD.lock. The -m autostash steps fail too, because they\n   update HEAD.\n - A also has to put the branch and HEAD in one transaction, which runs\n   into a reftable reflog bug I reported separately:\n   https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/\n - With B nothing is locked during the worktree update, so -m and other\n   processes work as before. A branch created by another process between\n   the check and the commit still leaves the switch halfway, and hooks see\n   the check as \"prepared\" followed by \"aborted\".\n\nSo I'd like to go with B. The tracking config would be written before the\nworktree update and removed if a later step fails. For -C on the current\nbranch HEAD does not change, so it would be left out of the check.\n\nDoes B sound acceptable, given that it does not cover the race with\nanother process?\n\nThanks,\nTsukasa\n\n"}]}