Deleting a badly named git branch
Asked Answered
W

7

67

I know this isn't strictly a programming question, but it is related to git. I accidentally have created a branch in git called --track (I got the order of options wrong when merging a remote branch)

The regular command doesn't work:

git branch -D "--track"  

I have tried to escape with quotes and backward slashes, however neither work.

Any ideas?

Worthen answered 28/7, 2009 at 6:13 Comment(4)
I'd like to know how you managed to create that branch in the first place. It doesn't look like you did "git branch -- --track". Or did you?Florin
Here is the offending line, I was trying to track a remote branch. git branch -b --track origin/devWorthen
Quotes or slashes don't work because they're interpreted earlier, by your shell, while the problem lies inside Git and its argument parsing.Soluk
Escaping/quoting won't help because the issue isn't with how the shell is interpreting the command, but with how git is interpreting it. All escaping is done before passing arguments to git.Haldeman
B
114

Did you try

git branch -D -- --track

? the "--" is usually the convention for "what follows is not an option, whatever its name"


From "The Art of Unix Programming", section "Command-Line Options":

It is also conventional to recognize a double hyphen as a signal to stop option interpretation and treat all following arguments literally.

You will find that convention in other (not necessary Unix-related) CLI (Command Line Interface) like cleartool:

If a nonoption argument begins with a hyphen () character, you may need to precede it with a double-hyphen argument, to prevent it from being interpreted as an option:

cleartool rmtype -lbtype -- -temporary_label- 

The P18 (a fast and flexible file preprocessor with macro processing capabilities and special support for internationalization) mentions that also and gives a good description of the general idea behind that convention:

All option arguments passed to the commands start with a single hyphen.
All option arguments (if any) must precede all non-option arguments.
The end of the option arguments may be signaled using a double hyphen, this is useful if a non-option argument starts with a hyphen. Terminating the list of option arguments with a double hyphen works for all commands, even those that don't take any option arguments.

The OptionParser tool written in ruby also lays it out quite plainly:*

Option Parsing Termination

It is convention that a double hyphen is a signal to stop option interpretation and to read the remaining statements on the command line literally. So, a command such as:

 app -- -x -y -z

will not ‘see’ the three mode-flags. Instead, they will be treated as arguments to the application:

 #args = ["-x", "-y", "-z"]

Note: sometimes, it takes three dashes and not two, especially when the CLI follows strictly the Gnu options styles:

The Gnu style command line options provide support for option words (or keywords), yet still maintain compatibility with the Unix style options.
The options in this style are sometimes referred to as long_options and the Unix style options as short_options.
The compatibility is maintained by preceding the long_options with two dashes

Similar to the Unix style double-hyphen ’--’, the Gnu style has a triple-hyphen ’---’ to signal that option parsing be halted and to treat the remaining text as arguments (that is, read literally from the command line)

So... if ' -- ' is not enough (it should be with Git commands), try ' --- '

Bumgarner answered 28/7, 2009 at 6:17 Comment(7)
Is -- a convention for git only or for most linux command line tools?Charnel
@Zelux: good question. I was just checking that. Not confirmed for now.Bumgarner
Perhaps not all, but certainly for such basics as rm. It's one way you get rid of a file called "-rf"....Planksheer
@quark, another way to get rid of “-rf” would be “rm ./-rf”. :)Ostensible
With msysgit, this doesn't work from Powershell, but does work from a cmd window.Egwin
If you try this and get "error: branch '--xxx' not found.", it could be because the hyphens are en dashes. Copying & pasting the branch name got the command to work. (no idea how I ended up in this situation)Olympium
@FraserHarris The hyphen-minus bug? (https://mcmap.net/q/11577/-what-39-s-the-toughest-bug-you-ever-found-and-fixed-closed) A classic indeed (as in, for instance, https://mcmap.net/q/11441/-create-clearcase-dynamic-view-using-cal-in-c)Bumgarner
F
8
git branch -D -- --track
Florin answered 28/7, 2009 at 6:18 Comment(0)
A
5

I'm using msysgit 1.7.0.2 and the suggested solution doesn't work:

git branch -D -- --track # doesn't work

No error is reported, but the branch still remains. I ended up forcibly removing the branch via:

rm .git/refs/heads/--track

Amylaceous answered 26/3, 2010 at 16:33 Comment(0)
S
2

The double hyphen didn't work for me on remote with a branch name containing double quotes and ampersands. However wrapping the name quotes and escaping the contained quotes did the job:

git push origin --delete "123-my-branch-&-some\"quoted-text\""

and locally:

git branch -D "123-my-branch-&-some\"quoted-text\""
Stalinist answered 5/11, 2013 at 10:6 Comment(0)
A
1

you can use software: sourcetree which can delete any branch you like.

Ascendancy answered 12/1, 2017 at 4:1 Comment(0)
S
0

I had a similar problem where I accidentally ended up with a "-r" branch. I couldn't figure out how to remove it using git commands so I just remove it in the .git folder:

$ cd .git/refs/head $ ls *r -r $ rm "*r"

This solution was only safe because it was the only branch listed that ended in "r" but it did solve the problem...

Stockroom answered 19/12, 2016 at 19:22 Comment(0)
P
0

If you're using an IDE like IntelliJ, you can delete the branch from the IDE. Just click on branch list, hover over the branch you want and then rename or delete it.

Poaceous answered 19/9, 2022 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.