User Created Image
User Created Image

Reputation: 1182

Delete broken branch in git

I have created branch with "git checkout -b mybranch". It seems that something went wrong, and now I can't delete it using "git branch -D mybranch". It says: error: branch 'mybranch' not found.

Upvotes: 28

Views: 15515

Answers (6)

GauRang Omar
GauRang Omar

Reputation: 871

I have the same problem git branch -d <branch_name> was not working, And I didn't found anything in .git/packed-refs and .git/refs/heads but I got files in

.git/refs/remotes/origin

with the name of the branches that I was not able to delete locally as well as in remote. But after deleting the files with the branch_name that I wanted to delete it was not showing in local.

To delete it on remote use

git fetch -p origin

The -p --prune option tells that before fetching, remove any remote-tracking references that no longer exist on the remote. Then use command

git push origin :<branch_name_you_was_unable_to_delete>

to delete on remote.

And you are done. :)

Upvotes: 2

Winand
Winand

Reputation: 2433

If branch name contains special characters it needs to be quoted:

$ git branch -D 'ENH-Adding-unit-``julian``-to-``to_datetime``'

Upvotes: 1

Łukasz Dumiszewski
Łukasz Dumiszewski

Reputation: 3038

I had the same problem. The branch was on the list of branches whenever I executed the git branch command, but I couldn't delete it.

The solution in my case was simple and a bit unexpected: I checked out the broken branch git checkout broken_branch (yes, it worked), then I checked out back to master and... again executed git branch -D broken_branch.

Upvotes: 1

solstice333
solstice333

Reputation: 3649

I used git update-ref -d refs/heads/<branch name> to fix this issue. Presumably, this does the same thing as what Rup suggests in the selected answer except it's interfaced via Git's CLI.

Upvotes: 9

Rup
Rup

Reputation: 34408

If git branch -D really isn't working, then your only option is to side-step it and edit the git check-out's state yourself. You should go to the root-directory of your check-out (where the .git directory is) and

  1. edit .git/packed-refs; if you see a line with your branch name then delete it
  2. look in .git/refs/heads for a file named after your branch; if you see one, delete it

Upvotes: 64

poke
poke

Reputation: 387647

You obviously don’t need to delete a branch that does not exist. Use git branch to see a list of branches, if it’s not in there, then there is no branch, and you don’t need to delete it. Otherwise make sure to type the name correctly and git branch -D should work.

Nevertheless you don’t need to care much about a broken branch that might be still around but is inaccessible. Branches in Git are in fact simple 40 bytes files, so not really something you need to worry about.

Upvotes: 1

Related Questions