git
runtime_error
ai_generated
true
错误:无法删除在 '/path/to/repo' 检出的分支 'main'
error: Cannot delete branch 'main' checked out at '/path/to/repo'
ID: git/cannot-delete-branch-checked-out
90%修复率
85%置信度
1证据数
2023-03-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| git 2.30 | active | — | — | — |
| git 2.39 | active | — | — | — |
| git 2.45 | active | — | — | — |
根因分析
尝试删除当前 Git 仓库中已检出的分支。
English
Attempting to delete the currently checked-out branch in a Git repository.
官方文档
https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---delete解决方案
-
Switch to another branch (e.g., 'main' or 'develop') before deleting: git checkout other-branch && git branch -D main
-
If the branch is not needed, force delete from a different worktree or after detaching HEAD: git checkout --detach && git branch -D main
无效尝试
常见但无效的做法:
-
95% 失败
Force delete (-D) also fails on the currently checked-out branch; Git refuses to delete a branch that is active.
-
90% 失败
The branch is still checked out in the current working tree; Git checks the repository state, not just the session.