git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: switch/checkout stops halfway when it fails to write tracking config

From
井井上司 <inoue.tsukasa.4@gmail.com>
Date
Oct 22, 2026, 23:48 UTC
Message-ID
<CAP8tz2+cJ4E+Nv_dHuY-8-DoGj7_nP1=98Nn5+M3qMb5WezYVw@mail.gmail.com>
In-Reply-To
<CALnO6CB=tmTrNKcWOanb79Bm9gPT5H_dntNGRJGp6iEYHe_aBg@mail.gmail.com>
> I wonder if it's a royal pain to make that step and the index step come
> later in the sequence? But that is more of a band-aid than a proper
> atomicity fix.

I checked this against the code and tried two cases. I used a build of the series (2.56.0.217.gfcc73a773a, master plus the series at https://lore.kernel.org/git/pull.2275.git.1792637495.gitgitgadget@gmail.com/).

Why the worktree update comes first: switch_branches() calls merge_working_tree() before update_refs_for_switch(). If merge_working_tree() fails, the function returns before the ref is created or HEAD is updated.

Case A: a local change to f conflicts with the switch, and I ran "git switch -c y origin/main". It fails with "Your local changes to the following files would be overwritten by checkout", exit 1. HEAD stays on main, the local change is kept, no ref y is created, and branch.y.remote is not set. Nothing is left behind.

If the ref and the tracking config were written first, this failure would leave a new branch and its config behind. Moving the worktree update later would move that cleanup into this case, so I agree that moving the step is a band-aid rather than atomicity.

Some failures can still happen after the worktree update:

Case B: with .git/refs/heads/y.lock present, "git switch -c y origin/main" fails with "fatal: cannot lock ref 'refs/heads/y'", exit 128. HEAD stays on main, and the switch's change to f stays staged. This is the same kind of halfway state. Updating HEAD can also fail: checkout.c:1027-1028 calls die(_("unable to update HEAD")).

The tracking-config failure is handled by the series linked above: the switch finishes and exits 0. Full atomicity, which would also undo the worktree and the ref when a later step fails, is left for a separate topic.

2026年10月22日(木) 21:48 D. Ben Knoble <ben.knoble@gmail.com>:
Show 38 quoted lines
>
> On Tue, Oct 20, 2026 at 11:09 AM 井上司 <inoue.tsukasa.4@gmail.com> wrote:
> >
> > > 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.
>
> Ah, thanks, I think that's what I overlooked. I wonder if it's a royal
> pain to make that step and the index step come later in the sequence?
> But that is more of a band-aid than a proper atomicity fix.
>
> > - 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.
>
> [trimmed repeated quoted content]
>
> --
> D. Ben Knoble
Previous: D. Ben KnobleNext: 井上司
Message 8 of 9 in “switch/checkout stops halfway when it fails to write tracking config”
  1. 井上司Oct 20, 2026
  2. Junio C HamanoOct 20, 2026
  3. 井上司Oct 20, 2026
  4. Junio C HamanoOct 21, 2026
  5. D. Ben KnobleOct 20, 2026
  6. 井上司Oct 20, 2026
  7. D. Ben KnobleOct 22, 2026
  8. 井上司Oct 22, 2026
  9. 井上司Nov 8, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.