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

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.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.