Bob Wakefield
Bob Wakefield

Reputation: 4009

How can I delete the current Git branch?

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.

Upvotes: 309

Views: 454625

Answers (18)

Hamza Muazzam
Hamza Muazzam

Reputation: 384

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

Upvotes: -1

aagargoura
aagargoura

Reputation: 396

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

Upvotes: 0

VonC
VonC

Reputation: 1329492

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.

Upvotes: 1

dkniffin
dkniffin

Reputation: 1383

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

Upvotes: 18

badambassador
badambassador

Reputation: 131

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.

Upvotes: 13

Guy s
Guy s

Reputation: 1806

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.

Upvotes: 16

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.

Upvotes: 71

Mubashar Hussain
Mubashar Hussain

Reputation: 307

git branch --D branch-name

Please run this code to delete

Upvotes: -2

Toxic Xander
Toxic Xander

Reputation: 31

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

Upvotes: 2

Sankar Natarajan
Sankar Natarajan

Reputation: 514

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>

Upvotes: 8

APartha77
APartha77

Reputation: 109

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

Upvotes: 5

Raskul
Raskul

Reputation: 2229

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

Upvotes: 4

Ger Hobbelt
Ger Hobbelt

Reputation: 1188

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

Upvotes: 2

Praveen Mitta
Praveen Mitta

Reputation: 1508

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.

Upvotes: 1

Vasu
Vasu

Reputation: 129

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

Upvotes: 3

jinxcat2008
jinxcat2008

Reputation: 743

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.

Upvotes: 37

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

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

Upvotes: 508

Randy Leberknight
Randy Leberknight

Reputation: 1463

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.

Upvotes: 27

Related Questions