-1

We all know git status command, and beginning of its output:

$ git status
On branch add_multiple_items_to_set__to_master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

the last mentioned line suggest that we should use -- to refer to last commit - HEAD.

I always wondered from where this come from. It took me a while to figured out, that I can use git checkout HEAD <file>... and expect this same result, and that git log -1 -- and git log -1 HEAD also is this same.

In which statements -- syntax are more natural? Are there any other multiple dashes shortcuts, like ---, etc.?

noisy
  • 6,495
  • 10
  • 50
  • 92
  • 1
    it would be great to hear some feedback from people who keeps downvoting my question. – noisy Jun 11 '15 at 13:33

2 Answers2

3

-- is not specific to Git, and it doesn't refer to HEAD.

It is a commonly used argument in Unixy command-line tools indicating the end of the options. Basically, it says "anything following me is a regular argument, not an option, even if it starts with - or --".

It's a way to let the tool operate on, say, a file called --foo:

git checkout --foo
# Um... I don't have an option called --foo. Time to bail out!

git checkout -- --foo
# Ooh, look! I'll operate on this perfectly valid file called --foo

Git just happens to default to using HEAD for many commands.

See also

Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
2

-- is just a separator which indicates that everything that comes after will be files.

So, when you are saying git checkout -- <file> You are doing the command git checkout specifically for the specified files. And when you do git checkout without specifying a branch/commit, the HEAD ref will be used as default.

So git checkout -- <file> is equivalent to git checkout HEAD -- <file> and the -- is just a separator

Alderath
  • 3,761
  • 1
  • 25
  • 43
  • is that right, that `--` is bash feature rather than git? – noisy Jun 11 '15 at 12:23
  • No, it't not a bash feature. Your shell does not process command line arguments to commands. It's just a standard behavior for programs that accept command line options. – larsks Jun 11 '15 at 14:40