Git development
 help / color / mirror / Atom feed
* switch/checkout stops halfway when it fails to write tracking config
@ 2026-10-20  1:39 井上司
  2026-10-20  4:11 ` Junio C Hamano
  2026-10-20 13:06 ` D. Ben Knoble
  0 siblings, 2 replies; 5+ messages in thread
From: 井上司 @ 2026-10-20  1:39 UTC (permalink / raw)
  To: git

1. What happened
When writing the tracking config fails (e.g. .git/config.lock exists) while
switch or checkout creates a new branch, the command exits with code 1 in the
middle of the switch. The worktree and index are already updated for the new
branch, but HEAD still points to the old branch. The user then sees unrelated
staged changes.

2. Reproduction
git init -q -b main up
cd up
echo one >file && git add file && git commit -q -m one
git branch feat
cd ..
git clone -q up work
cd up
git switch -q feat
echo two >>file && git commit -q -a -m two
cd ../work
git fetch -q
touch .git/config.lock
git switch feat; echo "exit=$?"
git symbolic-ref HEAD
git status --short

Output (git 2.43.0):
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
exit=1
refs/heads/main
M  file

3. Expectation
I expected it to either complete the switch, or fail without changing anything.

4. Scope
The reproduction above uses DWIM (git switch feat). The same happens with
switch -c, checkout -b, and --track. With -C on another existing branch, that
branch is reset before the failure, so its old commits can only be found in
the reflog.

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

6. Related
The wrong hint printed for the same failure is fixed separately in
<lore URL>. That patch does not fix this problem.

7. Next steps
I would like to work on a fix. Should checkout finish the switch and only
report the tracking error, or should it fail before touching the worktree?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-20 15:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-20  1:39 switch/checkout stops halfway when it fails to write tracking config 井上司
2026-10-20  4:11 ` Junio C Hamano
2026-10-20  6:33   ` 井上司
2026-10-20 13:06 ` D. Ben Knoble
2026-10-20 15:08   ` 井上司

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox