简体   繁体   中英

Deleting a local branch with Git

I have a branch called Test_Branch . When I try to delete it using the recommend method, I get the following error:

Cannot delete branch 'Test_Branch' checked out at '[directory location]'.

I get no other information besides that. I can blow away the remote branch easy but the local branch won't go away.

Switch to some other branch and delete Test_Branch , as follows:

$ git checkout master
$ git branch -d Test_Branch

If above command gives you error - The branch 'Test_Branch' is not fully merged. If you are sure you want to delete it The branch 'Test_Branch' is not fully merged. If you are sure you want to delete it and still you want to delete it, then you can force delete it using -D instead of -d , as:

$ git branch -D Test_Branch

To delete Test_Branch from remote as well, execute:

git push origin --delete Test_Branch

You probably have Test_Branch checked out, and you may not delete it while it is your current branch. Check out a different branch, and then try deleting Test_Branch.

Ran into this today and switching to another branch didn't help. It turned out that somehow my worktree information had gotten corrupted and there was a worktree with the same folder path as my working directory with a HEAD pointing at the branch ( git worktree list ). I deleted the .git/worktree/ folder that was referencing it and git branch -d worked.

如果您使用git worktree创建了多个工作,则需要先运行git prune才能删除分支

In my case there were uncommitted changes from the previous branch lingering around. I used following commands and then delete worked.

git checkout *

git checkout master

git branch -D

This worked for me...
I have removed the folders there in .git/worktrees folder and then tried "git delete -D branch-name".

Like others mentioned you cannot delete current branch in which you are working.

In my case, I have selected "Test_Branch" in Visual Studio and was trying to delete "Test_Branch" from Sourcetree (Git GUI). And was getting below error message.

Cannot delete branch 'Test_Branch' checked out at '[directory location]'.

Switched to different branch in Visual Studio and was able to delete "Test_Branch" from Sourcetree.

I hope this helps someone who is using Visual Studio & Sourcetree .

Ran into this problem today for branches reported by git branch -a and look like this one: remotes/coolsnake/dev , ie a local references to "remote" branches in registered git remote add ... remotes.

When you try

git branch -d remotes/coolsnake/dev

you'll get the error: branch 'remotes/coolsnake/dev' not found .

The magic is to remember here that these are references and MUST be deleted via

git update-ref -d remotes/coolsnake/dev

(as per https://superuser.com/questions/283309/how-to-delete-the-git-reference-refs-original-refs-heads-master/351085#351085 )

Took a while to uncover my mistake and only the fact that TortoiseGit could do it led me on the right path when I was stuck. Google DID NOT deliver anything useful despite several different approaches. (No that I've found what I needed there's also git: How to delete a local ref branch? , which did NOT show up as I had forgotten the 'ref' bit about these branches being references .)

Hope this helps someone else to get unstuck when they're in the same boat as me, trying to filter/selectively remove branches in a repo where some of them are present due to previous git remote add commands.


In a shell script this then might look like this:

#! /bin/bash
#
# Remove GNU/cesanta branches so we cannot accidentally merge or cherrypick from them!
# 

# commit hash is first GNU commit
R=218428662e6f8d30a83cf8a89f531553f1156d25

for f in $( git tag -l ; git branch -a ) ; do 
    echo "$f"
    X=$(git merge-base $R "$f" ) 
    #echo "X=[$X]" 
    if test "$X" = "$R" ; then 
        echo GNU 
        git branch -D "$f" 
        git update-ref -d "refs/$f" 
        git tag -d "$f" 
        #git push origin --delete "$f" 
    else 
        echo CIVETWEB 
    fi
done

Most junior programmers that faced

Cannot delete branch 'my-branch-name'

Because they are already on that branch.

It's not logical to delete the branch that you are already working on it.

Just switch to another branch like master or dev and after that delete the branch that you want:

git checkout dev

git branch -d my-branch-name

without force delete , you can delete that branch

If you run into this problem where you have checkedout and not able to delete the branch and getting this error message

"error: Cannot delete branch 'issue-123' checked out at ....."

Then check the branch you are currently in by using git branch

If the branch you are trying to delete is your current branch, you cannot delete the same. Just switch to the main or master or any other branch and then try deleting

git checkout main or master

git branch -d branchname git branch -D branchname git branch -D branchname --force

ran into same problem, checked out to different branch and used git branch -D branch_name worked for me

I had a similar problem except that the bad branch in the middle of rebase.
git checkout bad_branch && git rebase --abort solved my issue.

If any of the other answers did not work for you, this can also happen if you are in the middle of a rebase. So either finish the rebase or abort it:

git rebase --abort

Afterward, you can remove the branch.

git branch --D branch-name

Please run this code to delete

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