Home > Enterprise >  How can I delete a branch named '-delete'?
How can I delete a branch named '-delete'?

Time:03-31

I have accidentally created a local branch called -delete

When I try to run commands to delete the branch using

git branch -d -delete

git branch -D -delete

git branch --delete -delete

It does not work and gives the error message back that says this:

error: did you mean `--delete` (with two dashes ?)

How can I delete the '-delete' branch?

CodePudding user response:

No user-oriented Git command should have let you create that branch name in the first place (I had to resort to trickery to set up the condition myself, as I didn't think to use git update-ref right off), but once you have it, the way to get rid of the bad name is to use git update-ref:

git update-ref -d refs/heads/-delete

Here's my example:

$ git branch delete
$ mv .git/refs/heads/delete .git/refs/heads/-delete
$ git branch
  -delete
  diff-merge-base
* master
$ git branch -d -delete
error: did you mean `--delete` (with two dashes)?
$ git update-ref -d refs/heads/-delete
$ git branch
  diff-merge-base
* master

CodePudding user response:

git branch also honors the -- convention : if you pass -- alone as an argument on the command line, anything after that will not be interpreted as an option.

Starting from @torek's setup in his answer :

$ git branch delete && mv .git/refs/heads/delete .git/refs/heads/-delete
$ git branch
  -delete
* master

$ git branch -d -delete     # fails
error: did you mean `--delete` (with two dashes)?

$ git branch -d -- -delete  # works
Deleted branch -delete (was cec927c).

$ git branch
* master
  • Related