Fix "Cannot Delete Branch Used by Worktree" in Git

The Error

You run:

git branch -d feat/old-feature

And Git refuses:

error: Cannot delete branch 'feat/old-feature' checked out at '/home/user/myapp-old-feature'

Or with a variant:

fatal: Cannot delete branch 'feat/old-feature' which is checked out at '/home/user/myapp-old-feature'

This is not a bug. Git is protecting you. Here's exactly what's happening and how to fix it in under 60 seconds.


Why Git Blocks the Deletion

Every Git branch has a HEAD — a specific commit it points to. When a worktree has that branch checked out, its working directory reflects that HEAD. If Git let you delete the branch:

  1. The worktree would be on a branch that doesn't exist
  2. Any commits you make in that worktree would go to detached HEAD (easy to lose)
  3. Git's internal references (HEAD, ORIG_HEAD, reflog) would point to a ghost branch

So Git stops you. Safely.

The error message even tells you which worktree is holding the branch: checked out at '/home/user/myapp-old-feature'. That's the path you need to deal with.


If you're done with both the worktree and the branch (the common case):

# Step 1 — remove the worktree
git worktree remove /home/user/myapp-old-feature

# Step 2 — now delete the branch
git branch -d feat/old-feature

Done. Two commands.

If the worktree has uncommitted changes and Git refuses worktree remove, see Fix 3 or clean up properly.


Fix 2: Switch the Worktree to a Different Branch

If you want to keep the worktree folder but stop it from holding this specific branch:

# cd into the worktree
cd /home/user/myapp-old-feature

# Switch it to a different branch
git checkout main  # or any other branch

# Now go back and delete the original branch
cd -
git branch -d feat/old-feature

Useful when you're reusing the worktree for another task but want the old branch gone.


Fix 3: Force (When You Truly Don't Care)

If you know you want the branch deleted, even though a worktree references it, and you've accepted the consequences:

# If the worktree's folder has been deleted or moved manually:
git worktree prune

# Then delete the branch
git branch -d feat/old-feature

# If -d still complains (branch is unmerged), use -D:
git branch -D feat/old-feature

-D (capital) = force delete, even unmerged.

Warning: -D discards commits on that branch that aren't reachable from any other branch. If you do this without git reflog nearby, those commits are effectively lost.


Variation: The Worktree Folder Is Already Gone

Common scenario: you deleted the worktree folder with rm -rf, now Git still thinks it exists:

$ git branch -d feat/old-feature
error: Cannot delete branch 'feat/old-feature' checked out at '/home/user/myapp-old-feature'

$ ls /home/user/myapp-old-feature
ls: cannot access ...: No such file or directory

Fix: git worktree prune removes references to worktrees whose folders no longer exist:

git worktree prune
git branch -d feat/old-feature

If prune doesn't catch it (older Git versions, or the worktree was locked), manually inspect:

git worktree list
# Find the dead entry, then:
git worktree remove --force /path/that/is/gone

Variation: "fatal: not a working tree"

If you see this during git worktree remove:

fatal: '/home/user/myapp-old-feature' is not a working tree

It means Git doesn't recognize the path. Options:

# List what Git thinks exists:
git worktree list

# If your target path is there but with different capitalization or a trailing slash,
# copy the exact path from `list` and retry

# If the folder is gone:
git worktree prune

Variation: Locked Worktree

Some worktrees are locked (intentionally or not). You'll see:

fatal: '...' contains modified or untracked files, use --force to delete it

or:

error: ... is locked

Unlock first:

git worktree unlock /home/user/myapp-old-feature
git worktree remove /home/user/myapp-old-feature
git branch -d feat/old-feature

One-Liner: Nuke a Worktree + Branch Together

For when you know what you're doing and want it over with:

WT=/home/user/myapp-old-feature
BR=feat/old-feature
git worktree remove --force "$WT" && git branch -D "$BR"

Or as a shell function (add to your .zshrc/.bashrc):

wtkill() {
  local wt="$1"
  local branch="$2"
  git worktree remove --force "$wt" && git branch -D "$branch"
}
# Usage:
# wtkill /home/user/myapp-old-feature feat/old-feature

How to Avoid This Error in Future

  1. Always clean up worktrees when you finish a task — don't leave orphans
  2. Use git worktree list weekly — audit what's still around
  3. Never delete worktree folders with rm -rf — use git worktree remove
  4. Use a tool that tracks worktrees for youParallelCode shows all your worktrees in one dashboard with one-click delete

Stop managing worktrees by hand. ParallelCode visualizes every worktree across all your repos. Remove, rename, or archive with one click.

Download ParallelCode — Free →


Why You Might See This Error With ParallelCode / Claude Code

If you use Claude Code, Cursor Agent, or ParallelCode's worktree flow, this error shows up when:

  • An agent crashed mid-task and left a worktree behind
  • You deleted a task from ParallelCode's UI but skipped the cleanup step
  • Two tools both created worktrees for the same branch name

The fix is identical — git worktree remove followed by git branch -d. ParallelCode's "Delete task" button does both in one click; manual users need the two commands.


Summary: The 60-Second Fix

# 99% of cases:
git worktree remove <path-shown-in-error>
git branch -d <branch-name>

# Worktree folder gone?
git worktree prune
git branch -d <branch-name>

# Worktree locked?
git worktree unlock <path>
git worktree remove <path>
git branch -d <branch-name>

# Give up and force:
git worktree remove --force <path>
git branch -D <branch-name>