简体   繁体   中英

Having trouble deleting a local branch even after checkout to another branch

I'm trying to delete my local branch and tried most of the solutions I found on here which is to checkout to another branch and then run git branch -D (or -d) <my_branch> . I tried that but I am still getting the same error that states "Cannot delete branch 'my_branch' checked out at 'my_path'

How I got myself in this situation: I branched off of my develop branch by doing git worktree add -b branch_name ../project_name develop . Then I realized I wanted to change my branch name so I deleted the entire directory first by using rm- rf . Now my_path is pointing to a deleted directory so I'm not sure what to do now. Help will be appreciated. I'm running on Windows 7 using Git Bash

Things I have tried:

  1. Restarting my computer
  2. Reinstalling Git Bash
  3. Checkout to another branch and try git branch -d and git branch -D

Screenshot of error:git分支删除错误 The (virtualBDD) is my virtual environment. You can ignore that.

Did a bit more browsing and came across this answer that uses git worktree prune to remove information on (non-locked) worktrees that no longer exist. This essentially removed my worktree from git worktree list and then I proceeded to do git branch -d my_branch . This solved my issue. Thanks to everyone that helped.

You have a worktree for the branch in question checked out in another location and you can't delete that branch until you've removed the worktree.

Git doesn't let you remove a branch that has a worktree associated with it, since that would leave the worktree in a useless and broken state. If you want to delete the branch, you first need to use git worktree remove to remove the given worktree, possibly with -f , and then you'll be able to delete the branch. If you're not sure where your worktree is, you can use git worktree list to find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM