How to Delete a Local Git Branch Starting With a Hyphen?

Find out how to delete a local git branch that starts with hyphen(s)

In git, single and double hyphens are used to specify single character and multi-character flags/options respectively. Therefore, when you try to delete a local git branch that starts with one or more hyphens, it might not work. In those cases, you should instead use the "non-option argument" (--) — i.e. double hyphens without any flag/option specified — after the git delete command to specify the branch you wish to delete.

For example, to force-delete a local branch named "-f", you could use the following command:

git branch -D -- -f

Similarly, to force-delete a local branch called "--track" for example, you could do the following:

git branch -D -- --track

Use of the double hyphens (non-option argument) is a Unix CLI convention that is widely used as a signal to stop flag/option interpretation and treat all arguments that follow, literally.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.