How to delete a git working tree branch when its working directory has been removed?

You are using git worktree, so the answer is in the git worktree documentation:

When you are done with a linked working tree you can simply delete it. The working tree’s administrative files in the repository (see “DETAILS” below) will eventually be removed automatically (see gc.worktreePruneExpire in git-config(1)), or you can run git worktree prune in the main or any linked working tree to clean up any stale administrative files.

(emphasis mine). Git won’t let you delete the branch if it believes it is checked out in a secondary worktree. If the secondary worktree is already removed, but Git has not yet caught on to that fact, just run git worktree prune to tell Git to go check.

Leave a Comment