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?