Re: switch/checkout stops halfway when it fails to write tracking config
Show 5 quoted lines
> I can see where create_branch() is called prior to setting up HEAD,
> but I'm mildly confused about the index/wt updates prior to either! It
> seems to me that should be one of the last steps, hm. Unfortunately,
> I'm having a hard time finding the responsible code outside of the
> "merge" case (which probably has a similar issue).
- merge_working_tree() updates the worktree and the index. It is called
from switch_branches() on every branch switch, not only with -m,
because do_merge starts at 1.
- The "merge" here is not -m. It is the 2-way merge in unpack_trees().
-m only adds an autostash step for when that 2-way merge fails.
- The order is: update the worktree, create the ref, write the tracking
config (which fails), then update HEAD. As you noticed, the worktree
and index are updated before the ref and HEAD, so a failure later
leaves those changes in place.
- As you guessed, the -m case has the same problem. I tried
"git switch -m -c y origin/main" with the lock present (v2.56.0 plus
my hint patch, which only changes the hint text). It stops in the same
place. If local changes conflict, HEAD stays on main, the file is left
conflicted, and the changes remain in the stash.
- Running the same command again in that conflicted state stops with
"a branch named 'y' already exists". Switching to the existing branch
y stops with "you need to resolve your current index first"
(checkout.c:887 in v2.56.0). Neither run changes the state.
2026年10月20日(火) 22:07 D. Ben Knoble <ben.knoble@gmail.com>:
Show 18 quoted lines
>
> On Mon, Oct 19, 2026 at 9:41 PM 井上司 <inoue.tsukasa.4@gmail.com> wrote:
> >
> > 5. Root cause
> > update_refs_for_switch() -> create_branch() -> setup_tracking() fails to write
> > the config, and the code calls exit(1) in setup_tracking() (branch.c). At that
> > point the worktree and the branch have been changed, but HEAD has not. This
> > exit was introduced in 27852b2c53 (branch: report errors in tracking branch
> > setup, 2016-02-22).
>
> I can see where create_branch() is called prior to setting up HEAD,
> but I'm mildly confused about the index/wt updates prior to either! It
> seems to me that should be one of the last steps, hm. Unfortunately,
> I'm having a hard time finding the responsible code outside of the
> "merge" case (which probably has a similar issue).
>
> --
> D. Ben Knoble