git local branch named -D is created, how to delete it

Viewed 55

When I tried to remove branch with the command : git branch –D origin/image

I this message:

Branch '–D' set up to track remote branch 'image' from 'origin'.

and now a branch named "-D" is created locally buy does not exist globally. How can I remove it as it is a flag of git command and so not letting me delete with this command : git branch -D -D.

*using bitbucket and vscode.

2 Answers

Use -- to say -D is not meant as an option:

You can use the -- option to say the rest of the command line is not options, like most Unix/Linux (or Linux originating) commands allow:

git branch -d -- -D

But is it dash-D or hyphen-D?

If that didn't work, check if it's really a hyphen, or if it's one of the several Unicode dashes instead. You used both in the question interchangeably, which is why I ask.

I expect that this is the command you actually need to run, deleting the branch called dash-D (rather than hyphen-D):

git branch -d –D

Here I cut and pasted that –D from the message you got when you created the branch, and although it's subtle, I can confirm it's not a hyphen, it's U+2013 (en dash). You can cut and paste it from the output of git branch to make sure you get the exact same dash that's in the branch name.

And of course that would work with the -- too:

git branch -d -- –D

You have to can use a low-level command:

git update-ref -d refs/heads/-D
Related