git

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


I've removed its working directory, because this git working tree is no longer useful, then I git branch -D pubsub-sketch-tree at the directory of main repository. An error thrown:

error: Cannot delete branch 'pubsub-sketch-tree' checked out at '/Users/zhouhancheng/编程/github_own/sketch_worktree/pubsub-sketch_tree'  

But '/Users/zhouhancheng/编程/github_own/sketch_worktree/pubsub-sketch_tree' had been removed.


Solution

  • 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.