Skip to content

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

Open
tsukasaI wants to merge 3 commits into
gitgitgadget:maintfrom
tsukasaI:fix/branch-upstream-hint-shortname
Open

tsukasaI wants to merge 3 commits into
gitgitgadget:maintfrom
tsukasaI:fix/branch-upstream-hint-shortname

Conversation

@tsukasaI

@tsukasaI tsukasaI commented Oct 19, 2026 •

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. The hint
printed at that point was also wrong: it suggested
origin/refs/heads/.

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

Changes since v1:

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

@gitgitgadget

gitgitgadget Bot commented Oct 19, 2026

Copy link
Copy Markdown

Welcome to GitGitGadget

Hi @tsukasaI, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@tsukasaI

Copy link
Copy Markdown
Author

Hi, this is my first contribution to Git.
Could someone please /allow me?

This PR fixes a wrong hint shown by switch/checkout when writing the
tracking config fails.

@dscho

dscho commented Oct 19, 2026

Copy link
Copy Markdown
Member

/allow

@gitgitgadget

gitgitgadget Bot commented Oct 19, 2026

Copy link
Copy Markdown

User tsukasaI is now allowed to use GitGitGadget.

WARNING: tsukasaI has no public email address set on GitHub; GitGitGadget needs an email address to Cc: you on your contribution, so that you receive any feedback on the Git mailing list. Go to https://github.com/settings/profile to make your preferred email public to let GitGitGadget know which email address to use.

@tsukasaI

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Oct 20, 2026

Copy link
Copy Markdown

Submitted as pull.2268.git.1792458904027.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v1

To fetch this version to local tag pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v1

@gitgitgadget

gitgitgadget Bot commented Oct 20, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"tsukasa-ino via GitGitGadget" <gitgitgadget@gmail.com> writes:

>     hint:   git branch --set-upstream-to=origin/refs/heads/feat
>
> The hint says origin/refs/heads/feat, but the correct name is
> origin/feat. Running the command as printed fails, because the full
> ref name is not a valid remote-tracking branch name here.

Good observation.  Thanks for noticing.

> +test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '
> +	test_when_finished "git branch -D hint-local" &&
> +	test_when_finished "git update-ref -d refs/remotes/hintremote/hintbranch" &&
> +	test_config remote.hintremote.url . &&
> +	test_config remote.hintremote.fetch "+refs/heads/*:refs/remotes/hintremote/*" &&
> +	git update-ref refs/remotes/hintremote/hintbranch HEAD &&
> +	git branch hint-local &&
> +	test_when_finished "rm -f .git/config.lock" &&
> +	>.git/config.lock &&
> +	test_must_fail git -c core.configLockTimeout=0 \
> +		branch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&
> +	test_grep "git branch --set-upstream-to=hintremote/hintbranch\$" err &&
> +	test_grep ! "refs/heads/" err

Perhaps it makes sense to also test that the suggested command line
is accepted after all of the above?

> +'

Thanks.

@tsukasaI
tsukasaI force-pushed the fix/branch-upstream-hint-shortname branch from 14eac91 to f5524d1 Compare October 21, 2026 00:06
@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

Harald Nordgren wrote on the Git mailing list (how to reply to this email):

> When git switch fails to write the tracking config, it prints a hint
> with the command to set the upstream by hand. For example, if
> .git/config.lock exists:
>
>     error: could not lock config file .git/config: File exists
>     error: unable to write upstream branch configuration
>     hint:
>     hint: After fixing the error cause you may try to fix up
>     hint: the remote tracking information by invoking:
>     hint:   git branch --set-upstream-to=origin/refs/heads/feat
>
> The hint says origin/refs/heads/feat, but the correct name is
> origin/feat. Running the command as printed fails, because the full
> ref name is not a valid remote-tracking branch name here.

Very nice find!

> +test_expect_success '--set-upstream-to hint uses short remote-tracking branch name on locked config' '
> +       test_when_finished "git branch -D hint-local" &&
> +       test_when_finished "git update-ref -d refs/remotes/hintremote/hintbranch" &&
> +       test_config remote.hintremote.url . &&
> +       test_config remote.hintremote.fetch "+refs/heads/*:refs/remotes/hintremote/*" &&
> +       git update-ref refs/remotes/hintremote/hintbranch HEAD &&
> +       git branch hint-local &&
> +       test_when_finished "rm -f .git/config.lock" &&
> +       >.git/config.lock &&
> +       test_must_fail git -c core.configLockTimeout=0 \
> +               branch --set-upstream-to=hintremote/hintbranch hint-local 2>err &&
> +       test_grep "git branch --set-upstream-to=hintremote/hintbranch\$" err &&
> +       test_grep ! "refs/heads/" err
> +'

Would be good to test the full multiline hint message. After all, it's
the point of the topic.

Right now, you are asserting that the correct form
'hintremote/hintbranch' works, but we want to make sure that the hint
actually gives that.


Harald

@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

User Harald Nordgren <haraldnordgren@gmail.com> has been added to the cc: list.

When git switch fails to write the tracking config, it prints a hint
with the command to set the upstream by hand. For example, if
.git/config.lock exists:

    error: could not lock config file .git/config: File exists
    error: unable to write upstream branch configuration
    hint:
    hint: After fixing the error cause you may try to fix up
    hint: the remote tracking information by invoking:
    hint:   git branch --set-upstream-to=origin/refs/heads/feat

The hint says origin/refs/heads/feat, but the correct name is
origin/feat. Running the command as printed fails, because the full
ref name is not a valid remote-tracking branch name here.

This has been wrong since a3f40ec (branch: accept multiple upstream
branches for tracking, 2021-12-20).

Strip refs/heads/ from the hint, as the success message in the same
function already does. The git config --add line for multiple upstreams
still needs the full ref name, so it is not changed.

Signed-off-by: tsukasa inoue <inoue.tsukasa.4@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
tsukasaI force-pushed the fix/branch-upstream-hint-shortname branch from f5524d1 to be94ac7 Compare October 21, 2026 14:20
@tsukasaI tsukasaI changed the title branch: strip refs/heads/ from the --set-upstream-to hint checkout: finish switch when tracking config cannot be written Oct 21, 2026
@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

This branch is now known as ti/branch-set-upstream-hint-fix.

@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

This patch series was integrated into seen via git@6e1e80b.

@gitgitgadget gitgitgadget Bot added the seen label Oct 21, 2026
@tsukasaI

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

Submitted as pull.2268.v2.git.1792618518.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v2

To fetch this version to local tag pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2268/tsukasaI/fix/branch-upstream-hint-shortname-v2

@gitgitgadget

gitgitgadget Bot commented Oct 21, 2026

Copy link
Copy Markdown

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"tsukasa-ino via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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. The hint printed
> at that point was also wrong: it suggested origin/refs/heads/.
>
> 1/3 strips refs/heads/ from the hint. 2/3 makes setup_tracking() return an
> error instead of calling exit(). No behavior change yet. 3/3 makes switch
> and checkout finish the switch and exit with status 0, as push -u, clone and
> fetch already do. git branch still fails.
>
> Changes since v1:
>
>  * 1/3: the test now compares the whole hint message, as suggested by Harald
>    Nordgren. It also checks that the command in the hint works once the lock
>    is removed, as suggested by Junio
>    (https://lore.kernel.org/git/xmqq7bjcup46.fsf@gitster.g/).

OK.

>  * 2/3 and 3/3 are new. They implement the direction agreed on the bug
>    report thread (https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).

I am not sure if these two should be part of the same topic as the
first one, or they should be treated separately.  IOW, if the way
these two patches implement the fix proves to be contentious, does
it make sense to hold the change in 1/3 until 2&3/3 are resolved
(and vice versa), or can they be treated as two separate issues?

> 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.

Sensible.

@gitgitgadget

gitgitgadget Bot commented Oct 22, 2026

Copy link
Copy Markdown

井上司 wrote on the Git mailing list (how to reply to this email):

> I am not sure if these two should be part of the same topic as the
> first one, or they should be treated separately.  IOW, if the way
> these two patches implement the fix proves to be contentious, does
> it make sense to hold the change in 1/3 until 2&3/3 are resolved
> (and vice versa), or can they be treated as two separate issues?

Thanks for the question. I checked whether the patches can be split.

- 2/3 applies without 1/3.
- 3/3 conflicts with 1/3 only in t3200, because its new test is placed
  right after the one added by 1/3. The code does not depend on 1/3. The
  hint sentence in the 3/3 commit message assumes the corrected hint, but
  that is only a wording dependency.

So 1/3 can be taken on its own. 2/3 and 3/3 can be discussed separately,
and changes to them do not need to hold up 1/3. I will resend 2/3 and 3/3
as a separate series on top of the current master, so that 1/3 can be
applied first.

2026年10月22日(木) 7:20 Junio C Hamano <gitster@pobox.com>:
>
> "tsukasa-ino via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > 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. The hint printed
> > at that point was also wrong: it suggested origin/refs/heads/.
> >
> > 1/3 strips refs/heads/ from the hint. 2/3 makes setup_tracking() return an
> > error instead of calling exit(). No behavior change yet. 3/3 makes switch
> > and checkout finish the switch and exit with status 0, as push -u, clone and
> > fetch already do. git branch still fails.
> >
> > Changes since v1:
> >
> >  * 1/3: the test now compares the whole hint message, as suggested by Harald
> >    Nordgren. It also checks that the command in the hint works once the lock
> >    is removed, as suggested by Junio
> >    (https://lore.kernel.org/git/xmqq7bjcup46.fsf@gitster.g/).
>
> OK.
>
> >  * 2/3 and 3/3 are new. They implement the direction agreed on the bug
> >    report thread (https://lore.kernel.org/git/xmqqecdjsryk.fsf@gitster.g/).
>
> I am not sure if these two should be part of the same topic as the
> first one, or they should be treated separately.  IOW, if the way
> these two patches implement the fix proves to be contentious, does
> it make sense to hold the change in 1/3 until 2&3/3 are resolved
> (and vice versa), or can they be treated as two separate issues?
>
> > 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.
>
> Sensible.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants