Deleting a local branch with Git
GitGit Problem Overview
I've googled and there are several very long threads on this topic and none of them seem to help. I think I'm doing something wrong.
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.
Git Solutions
Solution 1 - Git
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
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
Solution 2 - Git
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.
Solution 3 - Git
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.
Solution 4 - Git
If you have created multiple worktrees with git worktree, you'll need to run git prune
before you can delete the branch
Solution 5 - Git
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 <branch name>
Solution 6 - Git
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
Solution 7 - Git
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
Solution 8 - Git
This worked for me...
I have removed the folders there in .git/worktrees folder and then tried "git delete -D branch-name".
Solution 9 - Git
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.
Solution 10 - Git
Ran into this problem today for branches reported by git branch -a
and look like this one: remotes/coolsnake/dev
, i.e. 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
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 https://stackoverflow.com/questions/18506456/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