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

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---delete

解决方案

  1. Switch to another branch (e.g., 'main' or 'develop') before deleting: git checkout other-branch && git branch -D main
  2. If the branch is not needed, force delete from a different worktree or after detaching HEAD: git checkout --detach && git branch -D main

无效尝试

常见但无效的做法:

  1. 95% 失败

    Force delete (-D) also fails on the currently checked-out branch; Git refuses to delete a branch that is active.

  2. 90% 失败

    The branch is still checked out in the current working tree; Git checks the repository state, not just the session.