What is the reason that in git we can refer to HEAD by --?

99 views Asked by At

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.?

2

There are 2 answers

0
Chris On BEST ANSWER

-- 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

2
Alderath On

-- 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