Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

diff

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.

like image 202
Logan Lee Avatar asked Jun 25 '26 14:06

Logan Lee


1 Answers

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

like image 169
torek Avatar answered Jun 28 '26 03:06

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!