2

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

By the way, git diff --cached is the same as git diff --cached HEAD here.

Thanks.

torek
  • 448,244
  • 59
  • 642
  • 775
Logan Lee
  • 807
  • 9
  • 21
  • Note: I removed the [tag:caching] tag as Git's index / staging-area / cache is not what the tag is really for... – torek Mar 12 '20 at 01:23

1 Answers1

4

For git diff, --staged and --cached are synonyms: use whichever you like.

Omitting HEAD generally implies the same thing as HEAD, so these are almost 100% identical, but there is one exception: in a new repository that has no commits yet, git diff --staged works and git diff --staged HEAD does not:

$ 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>...]'

(This is because git diff --[cached|staged] is clever enough to notice that HEAD is invalid, and to diff against the empty tree instead. If you type in HEAD yourself, though, git diff dutifully attempts to resolve it to a commit hash ID, which then fails, producing the above error.)

(This exception also applies when creating an orphan branch, which is functionally very similar to this initial-empty-repository state: the special HEAD file holds the name of a branch that does not exist, so HEAD itself is only valid when being resolved as a branch name, not when being resolved as a commit hash ID.)

torek
  • 448,244
  • 59
  • 642
  • 775