git runtime_error ai_generated true

error: Cannot delete branch 'main' checked out at '/path/to/repo'

ID: git/cannot-delete-branch-checked-out

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-03-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
git 2.30 active
git 2.39 active
git 2.45 active

Root Cause

Attempting to delete the currently checked-out branch in a Git repository.

generic

中文

尝试删除当前 Git 仓库中已检出的分支。

Official Documentation

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

Workarounds

  1. 95% success Switch to another branch (e.g., 'main' or 'develop') before deleting: git checkout other-branch && git branch -D main
    Switch to another branch (e.g., 'main' or 'develop') before deleting: git checkout other-branch && git branch -D main
  2. 85% success If the branch is not needed, force delete from a different worktree or after detaching HEAD: git checkout --detach && 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

中文步骤

  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

Dead Ends

Common approaches that don't work:

  1. 95% fail

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

  2. 90% fail

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