Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete broken branch in git

Tags:

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.

like image 894
User Created Image Avatar asked Jun 14 '12 08:06

User Created Image


People also ask

How do I force delete a branch?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

How do I delete a git branch?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

Why can't I delete my git branch?

A: Git doesn't allow you to delete a local branch if you have the branch checked out. If you're having trouble deleting a Git branch, make sure you Git checkout a branch other than the one you want to delete.


2 Answers

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
like image 166
Rup Avatar answered Sep 23 '22 14:09

Rup


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.

like image 37
solstice333 Avatar answered Sep 20 '22 14:09

solstice333