Skip to content

checkout: finish switch when tracking config cannot be written - #2275

Open
tsukasaI wants to merge 2 commits into
gitgitgadget:masterfrom
tsukasaI:fix/checkout-tracking-config-failure
Open

tsukasaI wants to merge 2 commits into
gitgitgadget:masterfrom
tsukasaI:fix/checkout-tracking-config-failure

Conversation

@tsukasaI

Copy link
Copy Markdown

When the tracking config cannot be written (for example, because
.git/config.lock exists), git switch -c and checkout -b stop halfway.
The worktree, the index and the new branch ref are already updated, but
HEAD stays on the old branch, and the changes are left staged.

  • 1/2 makes setup_tracking() return an error instead of calling exit().
    No behavior change yet.
  • 2/2 makes switch and checkout finish the switch and exit with status 0,
    as push -u, clone and fetch already do. git branch still fails.

These patches were first sent as part of v2 of a series that also fixed
the hint
(https://lore.kernel.org/git/pull.2268.v2.git.1792618518.gitgitgadget@gmail.com/).
Junio asked whether they can be treated separately from the hint fix
(https://lore.kernel.org/git/xmqq7bjalmpt.fsf@gitster.g/), and I replied
that they can
(https://lore.kernel.org/git/CAP8tz2KOXHLjc5V1o1tZZWSsgOjVrp-ekH7H5KszBGK+hRqwrg@mail.gmail.com/).
The direction was agreed in the bug report thread
(https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).

Making the switch fully atomic, so that a failure undoes the worktree,
the index, the ref and the config, is left for a separate topic.

cc: Harald Nordgren haraldnordgren@gmail.com

setup_tracking() calls exit(1) when it cannot write the tracking
configuration. This exit was introduced in 27852b2 (branch: report
errors in tracking branch setup, 2016-02-22). Because of it, callers
cannot continue after a tracking failure, even though the ref has
already been created.

Change setup_tracking() to return an error instead of exiting, and make
create_branch() and dwim_and_setup_tracking() return that value too.

This does not change behavior. Callers still exit with status 1 on
failure, as before. Changing checkout

Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>
When git switch -c <new> <remote>/<branch> (or checkout -b) cannot write
the tracking config, for example because .git/config.lock exists, the
worktree, the index and the new branch ref have already been updated.
Git then exits with status 1 while HEAD still points to the original
branch. The changes of the switch remain staged, and the post-checkout
hook does not run.

create_branch() now returns an error instead of exiting. Make checkout
ignore that error so that the switch completes and the command exits
with status 0. The error and the hint are still printed.

Match push -u, clone and fetch, which also print the error and continue.
The upstream can be set later with the command shown in the hint.

Keep git branch <new> <start> failing when the tracking config cannot be
written, so its behavior does not change; t3200 covers this. Making the
switch fully atomic, undoing the worktree, the index, the ref and the
config when the tracking config fails, is a separate topic.

Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>
@tsukasaI

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Oct 22, 2026

Copy link
Copy Markdown

Submitted as pull.2275.git.1792637495.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2275/tsukasaI/fix/checkout-tracking-config-failure-v1

To fetch this version to local tag pr-2275/tsukasaI/fix/checkout-tracking-config-failure-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2275/tsukasaI/fix/checkout-tracking-config-failure-v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant