2

I have created a file a gave it the name --. How can I add it to the git repository?

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        --

nothing added to commit but untracked files present (use "git add" to track)

I know that I could do git add ., but I don't want to. I want to know how to specifically add this file, named --.

I tried:

$ git add --
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?
$ git add "--"
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?
$ git add '"--"'
fatal: pathspec '"--"' did not match any files
$ git add \-\-
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

So, how to add a file named -- to prepare it for commit?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 2
    Why do you want a file called `--`? It is going to cause nothing but grief. If you add that one, will you also add `-f` and `-r` and just `-` too? Bad, bad idea. – Jonathan Leffler Jun 18 '15 at 15:55
  • 1
    @JonathanLeffler Breaking git? :-) Just experimenting, finding out how things are working. – Ionică Bizău Jun 18 '15 at 15:56
  • 2
    Fine. The problem with `--` is not exclusive to `git`; it is a widely used convention documented by POSIX. You go through the same mechanics as you do with any other filename that looks like an option. Use a full path `$PWD/--` or a relative path `./--` or what people have suggested — use `--` to mark the end of arguments. But the fact that `--` has a conventional meaning in Unix means that a file called `--` is going to cause you grief from now until the day you remove it (which, one hopes, is going to be today, now!). – Jonathan Leffler Jun 18 '15 at 15:59
  • possible duplicate of [Deleting a badly named git branch](http://stackoverflow.com/questions/1192180/deleting-a-badly-named-git-branch) – jub0bs Jun 18 '15 at 16:18

3 Answers3

6

You can use:

git add ./--

or:

git add $PWD/--

or even:

git add -- --

These are all standard techniques for dealing with inappropriately named files that look like options to a command but are actually files. The standard requirement for these tricks is for rm -f ./-- or mv ./-- ./something-meaningful.

You are strongly counselled against creating a file with the name -- ever.

It is a name with a standard meaning for Unix commands generally, based on a mandate from POSIX. Many commands use it to mark the end of the options and indicate that the 'file name' (non-option) parameters follow after the -- argument.

So almost every time you need to do something with the file, you are going to have to treat its name specially. It isn't worth it. Anybody trying to work with you on your repository is going to hate you for it. You can't even edit it: vim -- doesn't edit the file --! OK; there are ways to edit it, such as vim ./-- and vim -- --, but this is obnoxious. (You can't cat the file; you can't use less or more on the file; you can't cp or mv the file; you can't list the file with ls -l --, ...)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4
git add -- --

The first -- tells Git to stop parsing options and accept everything after as a filename.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
4

git add -- --.

The "--" is used in git commands to separate paths from options.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54