-2

If I modify a file and do git diff I see the diff in the terminal.

If I then stage the modified file and do git diff again, there is no diff. If I instead do git diff HEAD i see the diff in the terminal, like before staging it.

So what is git diff short-hand for? I thought it was the same as git diff HEAD but obviously not.

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 2
    Did you read the docs? [_"This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t."_](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgt--ltpathgt82308203) – jonrsharpe May 19 '21 at 13:22
  • 1
    https://git-scm.com/docs/git-diff#_description – ElpieKay May 19 '21 at 13:22

2 Answers2

2

git diff compares the working tree (the files on disk) to the index (the staged modifications).

AFAIK, it isn't the shorthand of any other compbination of arguments.


The examples section of the documentation explains pretty well the subtleties around the "compare the latest changes" commands :

Various ways to check your working tree

$ git diff            (1)
$ git diff --cached   (2)
$ git diff HEAD       (3)
  1. Changes in the working tree not yet staged for the next commit.
  2. Changes between the index and your last commit; what you would be committing if you run git commit without -a option.
  3. Changes in the working tree since your last commit; what you would be committing if you run git commit -a

In that list : (2) is a shorthand for git diff --cached HEAD, but I don't know of any other way to write (1) or (3).

LeGEC
  • 46,477
  • 5
  • 57
  • 104
2

The documentation summarizes pretty nicely.

For git diff

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.

And git diff HEAD will show you the difference between the index and HEAD being the last commit on your working tree.

But to quote right from the documentation:

Changes in the working tree since your last commit; what you would be committing if you run git commit -a

marcusshep
  • 1,916
  • 2
  • 18
  • 31