git diff --staged 是否与 git diff --cached HEAD 相同?

Is git diff --staged same as git diff --cached HEAD?

git diff --stagedgit diff --cached HEAD一样吗?

顺便说一句,git diff --cached和这里的git diff --cached HEAD是一样的。

谢谢。

对于git diff--staged--cached是同义词:随便用。

省略 HEAD 通常意味着与 HEAD 相同的事情,因此这些 几乎 100% 相同,但有一个例外:在一个新的没有提交的存储库,git diff --staged有效而git diff --staged HEAD不:

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   README.md

$ git diff --cached
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+test
$ git diff --cached HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

(这是因为 git diff --[cached|staged] 足够聪明,可以注意到 HEAD 无效,并与 empty tree 进行比较。如果您自己输入 HEAD ,但是,git diff 尽职尽责地尝试将其解析为提交哈希 ID,然后失败,产生上述错误。)

(创建孤立分支时也适用此异常,它在功能上与初始空存储库状态非常相似:特殊的 HEAD 文件包含不存在的分支名称,因此HEAD 本身仅在被解析为分支名称时有效,而不是在被解析为提交哈希 ID 时有效。)