在 git 中我们可以通过 -- 引用 HEAD 的原因是什么?

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

我们都知道git status命令,其输出的开头是:

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

最后提到的行建议我们应该使用 -- 来引用最后一次提交 - HEAD.

我一直想知道这是从哪里来的。我花了一段时间才弄清楚,我可以使用 git checkout HEAD <file>... 并期望得到相同的结果,并且 git log -1 --git log -1 HEAD 也是相同的。

哪些语句--语法更自然?是否还有其他多破折号快捷方式,如 --- 等?

-- 不特定于 Git,也不指代 HEAD

这是Unixy命令行工具中常用的参数,表示选项的结束。基本上,它说 "anything following me is a regular argument, not an option, even if it starts with - or --".

这是一种让工具操作的方法,比如说,一个名为 --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 恰好默认对许多命令使用 HEAD

另见

--只是一个分隔符,表示后面的所有内容都是文件。

所以,当你说 git checkout -- <file> 您正在专门针对指定文件执行命令 git checkout。 当你在没有指定 branch/commit 的情况下执行 git checkout 时,HEAD ref 将被用作默认值。

所以 git checkout -- <file> 等同于 git checkout HEAD -- <file>-- 只是一个分隔符