error: cannot delete branch 'feature' used by worktree at '...' means the
branch is checked out somewhere: either in the folder you are standing in, or in a
second working tree made with git worktree add. Switch that working tree to
another branch, or remove the extra worktree, and the delete goes through.
Read the path at the end of the message. If it is your own repository folder, you are
trying to delete the branch you are on: git switch main first. If it is
another folder, that is a linked worktree: git worktree remove it.
The error
Deleting the branch you are on:
PS> git switch -q feature
PS> git branch -d feature
error: cannot delete branch 'feature' used by worktree at 'C:/.../b3'
Deleting a branch that a linked worktree has checked out:
PS> git worktree add ..\b3-wt feature
Preparing worktree (checking out 'feature')
HEAD is now at 8b5c4e0 first
PS> git branch -d feature
error: cannot delete branch 'feature' used by worktree at 'C:/.../b3-wt'
The long absolute paths were shortened; nothing else changed.
Why it happens
Every working tree has its own HEAD, and a checked-out branch is the name
that HEAD points at. Deleting it would leave that working tree on a branch
that no longer exists, so Git refuses for any working tree, the main one included. Git
2.45.2 prints the same "used by worktree" wording in both cases, which is confusing when
you never created a worktree: your ordinary repository folder counts as the main one.
git worktree list shows every working tree and the branch it holds, so it
tells you at once where the branch is checked out (here a second worktree holding a
branch y):
PS> git worktree list
C:/.../b3 8b5c4e0 [main]
C:/.../b3-wt2 ac1e530 [y]
The fix
Branch checked out in your own folder: move off it, then delete.
PS> git switch -q main
Branch checked out in a linked worktree: remove that worktree, then delete.
PS> git worktree remove ..\b3-wt
PS> git branch -d feature
Deleted branch feature (was 8b5c4e0).
git worktree remove deleted the linked folder as well (a
Test-Path ..\b3-wt afterwards returned False), so commit or copy
anything you want from that folder first. If you would rather keep the folder, open it
and switch it to another branch instead; the branch is then free to delete.
How it was reproduced
Git for Windows 2.45.2 on Windows 11, Windows PowerShell 5.1, in a throwaway repository
with branches main and feature. git branch -d
feature while on feature exited with code 1; after switching to
main and adding a linked worktree for feature it failed again
with the worktree's path. After git worktree remove the delete succeeded.
Frequently asked
- How do I delete the branch I am currently on in git?
- You cannot; Git refuses with cannot delete branch used by worktree. Switch to another branch with git switch main, then run git branch -d again.
- What does used by worktree mean when I never created a worktree?
- Your normal repository folder is the main worktree. The message names its path when the branch you want to delete is the one checked out there.
- How do I remove a git worktree?
- Run git worktree remove with the worktree's folder path. In the test it deleted the folder too, after which the branch it had checked out can be deleted.
More decoded errors in the Fixes category. If the delete is refused because of unmerged commits instead, see the branch is not fully merged.