3

In git add how can you escape a leading "-" character in the filename? e.g.:

git add -index-apache-.html

gives:

error: unknown switch `d'

Thanks!

PS. same for

git checkout
pebox11
  • 3,377
  • 5
  • 32
  • 57
  • 3
    Try `git add -- -index-apache-.html`. For more details, see [this answer](http://stackoverflow.com/a/13321491/2541573). – jub0bs Sep 06 '14 at 18:39

2 Answers2

6

Use the -- to get around this issue. Anything past the double-dash is treated as just a filename.

This is more a Bash convention than a Git convention, as -- traditionally signifies the end of options.

A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

As an alternative to the general -- syntax,

git add -- -index-apache-.html

you could also use

git add ./-index-apache-.html

This works because the argument is a filename. Prepending ./ says "look for this file in the current directory", so it does not change the meaning of the filename (git would've looked there anyway), but it also means the argument no longer starts with - and cannot be confused with option switches.

melpomene
  • 84,125
  • 8
  • 85
  • 148