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

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

From
井井上司 <inoue.tsukasa.4@gmail.com>
Date
Oct 20, 2026, 01:39 UTC
Message-ID
<CAP8tz2JPXzq=Skokn4OtH8t6c3b+p1Wny1iX6AhXReUGvsBhuw@mail.gmail.com>
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?
Next: Junio C Hamano
Message 1 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.