How to fix error: Cannot delete branch 'feature' checked out at '/path/to/repo' in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 273 views

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.

Quick fix

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

# See your current branch
git status

# Or use git branch to see all branches with current marked
git branch
  1. Check which branch you're currently on
  2. Switch to a different branch before deleting
  3. Check for Git worktrees
  4. Remove the worktree to free the branch
git โ€” error
# See your current branch
git status
# Or use git branch to see all branches with current marked
git branch
error: Cannot delete branch 'feature' checked out at '/path/to/repo'

What this error means

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.

Typical symptoms

  • Running 'git branch -d <branch>' fails with 'Cannot delete branch' error
  • Error message shows a path that may not be your current directory
  • Force delete with 'git branch -D' also fails with the same error
  • The branch appears in 'git branch' output but cannot be removed
  • Switching branches doesn't resolve the error (worktree case)
  • CI/CD cleanup scripts fail when trying to delete feature branches

Common causes

  1. You are currently on the branch you're trying to delete
  2. The branch is checked out in a separate Git worktree
  3. A worktree directory was manually deleted but Git still tracks it
  4. An interrupted rebase or bisect operation has the branch checked out
  5. Multiple terminal sessions have the same repo open with different branches

How to fix "error: Cannot delete branch 'feature' checked out at '/path/to/repo'"

1Check which branch you're currently on

First, verify your current branch and the state of your repository:

bash
# See your current branch
git status

# Or use git branch to see all branches with current marked
git branch

The output will show your current branch with an asterisk (*):

bash
main
* feature    <-- You're on this branch
  develop

If you're on the branch you want to delete, you'll need to switch to a different branch first.

2Switch to a different branch before deleting

If you're on the branch you want to delete, switch to another branch first:

bash
# 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 feature

If the branch hasn't been merged and you're sure you want to delete it:

bash
# Force delete an unmerged branch
git branch -D feature

Important: 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.

3Check for Git worktrees

If switching branches doesn't resolve the error, the branch may be checked out in a separate worktree. List all worktrees:

bash
# List all worktrees
git worktree list

Sample output:

bash
/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.

4Remove the worktree to free the branch

If the branch is checked out in a worktree you no longer need, remove the worktree first:

bash
# 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 feature

If you still need the worktree but want to delete that branch:

bash
# 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 feature
5Clean up stale worktree references

If 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:

bash
# 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 feature

The prune command cleans up stale worktree references from the .git/worktrees directory.

6Check for interrupted rebase or bisect

An in-progress rebase or bisect operation can lock a branch. Check your repository status:

bash
git status

If you see "rebase in progress" or "You are currently bisecting":

For rebase:

bash
# Complete or abort the rebase
git rebase --continue
# Or
git rebase --abort

For bisect:

bash
# End the bisect session
git bisect reset

After resolving the in-progress operation, try deleting the branch again.

7Use detached HEAD as a workaround

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:

bash
# 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 feature

Warning: In detached HEAD state, any new commits won't belong to a branch. Create or checkout a branch before doing further work:

bash
# Create a new branch from the current state
git checkout -b new-branch

Advanced notes

Understanding 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

bash
# 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-hotfix

Branch 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:

bash
# Inspect worktree metadata
ls -la .git/worktrees/

# Manual cleanup (use with caution)
rm -rf .git/worktrees/stale-worktree

Deleting Remote Branches:

The "checked out" error only applies to local branches. You can always delete remote branches:

bash
# Delete a remote branch
git push origin --delete feature

# Or using the shorter syntax
git push origin :feature

Scripting Branch Deletion:

When writing scripts that clean up branches, handle this error gracefully:

bash
#!/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.

Frequently asked questions

What does "error: Cannot delete branch 'feature' checked out at '/path/to/repo'" mean?
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.
How do I fix "error: Cannot delete branch 'feature' checked out at '/path/to/repo'"?
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.
What causes "error: Cannot delete branch 'feature' checked out at '/path/to/repo'"?
You are currently on the branch you're trying to delete The branch is checked out in a separate Git worktree A worktree directory was manually deleted but Git still tracks it
Is the "error: Cannot delete branch 'feature' checked out at '/path/to/repo'" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading