302

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.

6

19 Answers 19

502

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
6
  • 71
    Switching branches was what got me - good catch! You can't delete the branch if you are active in it. ;-) Commented Jul 4, 2017 at 14:35
  • 14
    If you try a few different checkouts and still get the same message, look at the output of git status, or git branch; if you are rebasing Test_branch then simply checking out a different branch/commit will not fix this. Instead git rebase --abort then git checkout --detach and finally git branch -D Test_branch
    – droid
    Commented Jul 7, 2017 at 21:16
  • 6
    Further evidence that git stands for "git is terrible". The more I use it the more of these ridiculous hoops you have to jump through. Especially since you probably want to switch right back to the same branch.
    – user949300
    Commented Mar 10, 2018 at 22:10
  • And what if someone gets this in return, when trying to change branches? error: you need to resolve your current index first and they just want to LEAVE? EDIT - Solution: git reset --; git checkout -D [branch_name] Phew..
    – Paul
    Commented Oct 13, 2018 at 0:22
  • 1
    Had to do run git fetch --prune because I had already deleted the branch on remote but it was still showing up after git pull in git branch -a.
    – hipokito
    Commented Aug 27, 2020 at 11:49
62

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

3
  • Force delete didn't work for me, as corruption in rebase was the reason. This solution worked. +1. Commented Oct 11, 2023 at 5:39
  • 1
    This works for me. If you tried to merge other_branch to the bad_branch before and you forgot it, it will say you have checked out the bad_branch even if you are not. After abort the merge process, the bad_branch can be deleted by -d or -D.
    – Liam
    Commented Nov 2, 2023 at 6:26
  • Thanks! My IDE said a rebase was in progress on a completely unrelated branch, which is what lead me to this answer. git didn't (obviously?) seem to mind, which is what made the problem even harder to track down/understand.
    – tresf
    Commented Dec 18, 2023 at 19:16
37

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.

1
  • 1
    This answer should be the accepted answer in my opinion, because when I encountered the error the simple git branch -D solution did not work for me - the error was persistent. Commented Aug 18, 2023 at 9:38
27

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.

0
18

If you have created multiple worktrees with git worktree, you'll need to run git worktree prune before you can delete the branch

3
  • 4
    more specifically, you will need to use git worktree prune
    – Sergioet
    Commented Aug 9, 2018 at 14:57
  • but read what git prune does, first - could lead to data loss Commented Apr 22, 2019 at 12:46
  • 1
    Rather than using git worktree prune, I would suggest using git worktree remove <worktree> to be sure that you do not delete more work-tree information than you need to. If you have doubts, read the documentation that is output from this command: git help worktree Commented May 3, 2021 at 18:38
16

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.

12

In my case, I had a bisect I guess I didn't reset. Switching to the offending branch, running git bisect reset, and then switching to a different branch allowed the offending branch to then be deleted.

0
7

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>
5

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

4

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

2
  • 6
    Most junior programmers make better error messages than "Cannot delete branch" Commented Jun 26, 2021 at 1:16
  • 2
    Though to be fair - the message is "Cannot delete branch ... checked out at ...". "checked out" is the proper way to describe "being on that branch" Commented Nov 18, 2021 at 9:30
3

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

2

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

(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
2

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

1

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.

1

badambassador's answer mentions the case of a git bisect in-progress in a worktree.

One reason a git bisect can remain incomplete is because it remains stuck on a commit needing to check out a branch in a worktree... while that same branch is also checked out in another worktree.

To avoid that scenario, you can use a new option with Git 2.41 (Q2 2023), which allows "git bisect reset"(man) to check out the original branch when the branch is already checked out in a different worktree linked to the same repository.

See commit 7fb8904 (22 Jan 2023) by Rubén Justo (rjusto).
(Merged by Junio C Hamano -- gitster -- in commit c79786c, 19 Mar 2023)

bisect: fix "reset" when branch is checked out elsewhere

Signed-off-by: Rubén Justo

Since 1d0fa89 ("checkout: add --ignore-other-wortrees", 2015-01-03, Git v2.5.0-rc0 -- merge listed in batch #2) we have a safety valve in checkout/switch to prevent the same branch from being checked out simultaneously in multiple worktrees.

If a branch is bisected in a worktree while also being checked out in another worktree; when the bisection is finished, checking out the branch back in the current worktree may fail.

Let's teach bisect to use the "--ignore-other-worktrees" flag.

So you can now do a git bisect reset --ignore-other-worktrees (to clean up the current bisect), and then change branches to allow the deletion of the current branch in the main repository.

0

First you need to checkout other branch

git chekcout master

and then delete the required branch

git branch -d Test_Branch
0

run git worktree prune before you can delete the branch, if you have created multiple worktrees

0

//first checkout to other branch

git checkout master

// delete branch locally

git branch -d localBranchName

// delete branch remotely

git push origin --delete remoteBranchName

// refresh branch list

git fetch -p
-2
git branch --D branch-name

Please run this code to delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.