Path of Least Resistance
The most common use case for git diff
is when you've just made some changes to your working tree – the files you're currently working on – and you want to see those changes that could be further added to the next commit. You haven't staged them yet, which indicates you haven't determined them to be good enough for the next commit yet. In order to help you make that determination, you might want to run a git diff
to see what those changes are exactly.
The files that have already been added to the index – the staging area for the next commit – have (presumably) already been reviewed, and determined to be ready and acceptable for the next commit.
In order to make it as easy as possible to check the changes that you've just made (without being distracted by the changes that you're already okay with being in the next commit), the default behavior of git diff
using no other options is to check the working tree against the index:
git diff [<options>] [--] [<path>…]
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. You can stage these changes by
using git-add[1].
https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgt--ltpathgt82308203
Much Less Common
Of course, even if you have reviewed some changes and deemed them ready to go into the next commit, you may still want to double check what those changes are before committing them. Because this case is less common, it rightfully takes an additional option to perform it:
git diff [<options>] --cached [<commit>] [--] [<path>…]
This form is
to view the changes you staged for the next commit relative to the
named <commit>. Typically you would want comparison with the latest
commit, so if you do not give <commit>, it defaults to HEAD. If HEAD
does not exist (e.g. unborn branches) and <commit> is not given, it
shows all staged changes. --staged is a synonym of --cached.
https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgt--cachedltcommitgt--ltpathgt82308203
Other Examples from the Documentation:
Various ways to check your working tree
$ git diff (1)
$ git diff --cached (2)
$ git diff HEAD (3)
- Changes in the working tree not yet staged for the next commit.
- Changes between the index and your last commit; what you would be committing if you run
git commit
without -a
option.
- Changes in the working tree since your last commit; what you would be committing if you run
git commit -a
https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-Variouswaystocheckyourworkingtree
Probably Unecessary Side Note
In case it seems odd that you would check the working tree against the index to see the changes that you've just made (even when the index is empty), rather than checking it against the HEAD – a reference to the most recent commit on the current branch (i.e. what you've just made changes to) – you might want to consider what happens when an index is committed: the resulting, new commit is the contents of HEAD with the addition (or subtraction) of the changes in the index. A hypothetical commit with no changes staged in the index will still be the contents of HEAD and the additions (or subtractions) from index; it's just that the index consists of no changes (i.e. HEAD + index = HEAD
when index == 0
). So, with that in mind, git diff
with no options, and with no changes staged in the index, will be displaying changes in the working tree against the staging area for the next commit, but this effectively amounts to checking the working tree against HEAD, rendering the 1st and 3rd of those previous examples equivalent in this case.