This error occurs when you try to delete a Git branch that is currently checked out in your working directory or in a linked worktree. Git prevents this to avoid leaving your repository in an undefined state. The solution is to switch to a different branch first, or remove the associated worktree if using multiple working directories.
The "Cannot delete branch checked out" error is a safety mechanism in Git that prevents you from deleting a branch you're currently working on. When you're on a branch, Git has that branch's commit as your current HEAD, and your working directory contains that branch's files. If Git allowed you to delete the checked-out branch, your working directory would be left in an inconsistent state: you'd have files from a branch that no longer exists, and Git wouldn't know which commit your working directory represents. This error can appear in two scenarios: 1. **Direct checkout**: You're trying to delete the branch you're currently on in your main working directory. 2. **Worktree checkout**: The branch is checked out in a secondary worktree. Git's worktree feature allows you to have multiple working directories attached to a single repository, each with a different branch checked out. You cannot delete a branch that has any worktree associated with it. The error message helpfully tells you the path where the branch is checked out, which helps you identify whether it's your current directory or a separate worktree location.
First, verify your current branch and the state of your repository:
# See your current branch
git status
# Or use git branch to see all branches with current marked
git branchThe output will show your current branch with an asterisk (*):
main
* feature <-- You're on this branch
developIf you're on the branch you want to delete, you'll need to switch to a different branch first.
If you're on the branch you want to delete, switch to another branch first:
# Switch to main branch
git checkout main
# Or using the newer switch command (Git 2.23+)
git switch main
# Now delete the branch
git branch -d featureIf the branch hasn't been merged and you're sure you want to delete it:
# Force delete an unmerged branch
git branch -D featureImportant: The -D flag (uppercase) bypasses the "not fully merged" safety check, but it does NOT bypass the "checked out" protection. You must always switch branches first.
If switching branches doesn't resolve the error, the branch may be checked out in a separate worktree. List all worktrees:
# List all worktrees
git worktree listSample output:
/home/user/project abc1234 [main]
/home/user/project-feature def5678 [feature]The branch you're trying to delete might be checked out in another worktree location. The error message path should match one of these worktree paths.
If the branch is checked out in a worktree you no longer need, remove the worktree first:
# Remove a worktree (this doesn't delete the branch)
git worktree remove /path/to/worktree
# Or if the worktree directory is already deleted
git worktree prune
# Now you can delete the branch
git branch -d featureIf you still need the worktree but want to delete that branch:
# Navigate to the worktree
cd /path/to/worktree
# Switch to a different branch in that worktree
git checkout main
# Go back to main repo and delete the branch
cd /path/to/main/repo
git branch -d featureIf you manually deleted a worktree directory (e.g., using rm -rf) without using git worktree remove, Git may still think the branch is checked out:
# Prune references to worktrees that no longer exist
git worktree prune
# Verify the worktree is gone
git worktree list
# Now try deleting the branch again
git branch -d featureThe prune command cleans up stale worktree references from the .git/worktrees directory.
An in-progress rebase or bisect operation can lock a branch. Check your repository status:
git statusIf you see "rebase in progress" or "You are currently bisecting":
For rebase:
# Complete or abort the rebase
git rebase --continue
# Or
git rebase --abortFor bisect:
# End the bisect session
git bisect resetAfter resolving the in-progress operation, try deleting the branch again.
If you need to delete the branch but can't switch to another branch (for example, if it's the only branch), you can detach HEAD first:
# Detach HEAD by checking out the current commit
git checkout --detach
# Or checkout a specific commit
git checkout HEAD~0
# Now delete the branch
git branch -d featureWarning: In detached HEAD state, any new commits won't belong to a branch. Create or checkout a branch before doing further work:
# Create a new branch from the current state
git checkout -b new-branchUnderstanding Git Worktrees:
Git worktrees allow you to have multiple working directories for a single repository. Each worktree has its own checked-out branch. This is useful for:
- Working on multiple features simultaneously
- Building/testing one branch while developing on another
- Reviewing PRs without stashing your current work
# Create a new worktree
git worktree add ../project-hotfix hotfix-branch
# List all worktrees
git worktree list
# Remove a worktree properly
git worktree remove ../project-hotfixBranch Protection in Worktrees:
Git prevents deleting any branch that is the HEAD of any worktree. This is by design: deleting a branch while it's checked out would corrupt the worktree's state.
The .git/worktrees Directory:
Information about worktrees is stored in .git/worktrees/. Each worktree has a subdirectory containing:
- HEAD: The checked-out commit
- gitdir: Path to the worktree's .git file
- commondir: Reference to the main repository
If you experience persistent issues:
# Inspect worktree metadata
ls -la .git/worktrees/
# Manual cleanup (use with caution)
rm -rf .git/worktrees/stale-worktreeDeleting Remote Branches:
The "checked out" error only applies to local branches. You can always delete remote branches:
# Delete a remote branch
git push origin --delete feature
# Or using the shorter syntax
git push origin :featureScripting Branch Deletion:
When writing scripts that clean up branches, handle this error gracefully:
#!/bin/bash
branch="feature"
# Check if branch is checked out in any worktree
if git worktree list | grep -q "\[$branch\]"; then
echo "Branch $branch is checked out in a worktree"
exit 1
fi
# Check if it's the current branch
current=$(git rev-parse --abbrev-ref HEAD)
if [ "$current" = "$branch" ]; then
echo "Cannot delete current branch, switching to main"
git checkout main
fi
git branch -d "$branch"Bare Repositories:
In bare repositories (like those on Git servers), there's no working directory, so you won't encounter this error. However, you also can't use git checkout in bare repos.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server