Home › Git

error: Cannot delete branch checked out at (Git) — how to fix

error: Cannot delete branch 'feature/login' checked out at '/home/me/project'

Quick fix: Switch to another branch first, then delete: git switch main && git branch -d feature/login (use -D if it is not merged). If the path in the message is a different folder, it's a worktree: remove it with git worktree remove <path> (or git worktree prune if the folder is already gone).

What it means

git branch -d <name> refuses to delete a branch that some working tree currently has checked out, because that working tree's HEAD would then point to nothing. The path in the message tells you which working tree is using it:

Fix 1: switch away, then delete

git switch main          # or: git checkout main
git branch -d feature/login

If git answers error: the branch 'feature/login' is not fully merged, the branch has commits that aren't in your current branch or its upstream. Make sure you don't need them, then force:

git branch -D feature/login

If you have uncommitted changes that block switching, commit or stash them first (git stash).

Fix 2: the branch is checked out in another worktree

List worktrees:

git worktree list
/home/me/project              a1b2c3d [main]
/home/me/project-login        d4e5f6a [feature/login]

Either remove that worktree (it must be clean, or add --force):

git worktree remove /home/me/project-login
git branch -d feature/login

or switch the other worktree to a different branch:

git -C /home/me/project-login switch main

Fix 3: the worktree folder was deleted manually

If you deleted the worktree directory with rm -rf, git still has its metadata in .git/worktrees/. Clean it up:

git worktree prune
git branch -d feature/login

git worktree list should no longer show the missing path.

Deleting the remote branch too

git branch -d only deletes your local branch. To delete it on the remote:

git push origin --delete feature/login

and to drop stale remote-tracking refs for branches deleted by others:

git fetch --prune

IDE note

Editors such as VS Code or JetBrains IDEs may keep a worktree or a checkout open in another window. If git worktree list shows a path you don't recognise, check whether a tool created it.

Last updated 2026-12-02.