56

I quote a git tutorial:

git diff shows the diff between HEAD and the current project state

I wonder what that means. Isn't the HEAD the current active project?

Thanks

never_had_a_name
  • 90,630
  • 105
  • 267
  • 383

2 Answers2

130

From "Specifying revisions"

HEAD names the commit on which you based the changes in the working tree.

There are other heads (FETCH_HEAD, ORIG_HEAD and MERGE_HEAD). See Jefromi's answer for more.

The thing is, by default git diff actually shows the differences between "the current state of your project" (i.e. your files on your working tree) and the index (not HEAD).
In other words, the differences are what you could tell git to further add to the index but you still haven't.

if you do git diff --cached, then it will compare the index with HEAD.

See git book for more (archive link):

A common use is to simply run

$ git diff

which will show you changes in the working directory that are not yet staged for the next commit. If you want to see what is staged for the next commit, you can run

$ git diff --cached

which will show you the difference between the index and your last commit; what you would be committing if you run "git commit" without the "-a" option. (In Git versions 1.6.1 and later, you can also use git diff --staged which may be easier to remember.) Lastly, you can run

$ git diff HEAD

which shows changes in the working directory since your last commit; what you would be committing if you run "git commit -a".

See also 365git: Getting a diff between the working tree and other commits:

http://images.abizern.org.s3.amazonaws.com/365git/Feb11/Git%20Diff%202.png

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    awesome link, in the 3 years of using git I haven't seen a single image that explained stuff so well. The offical git images are horrible compared to the simplicity provided by the linked image. All other images on the blog seem to be comparably awesome. I mean the official git images are rather image series which still leave room for interpretation, while the image in your post explains 5 different commands in only 1 image. This is quite the inversion of efficiency, 5 images per command compared 5 commands per image. – HopefullyHelpful Jun 29 '17 at 13:29
  • I don't understand this at all. Let's say I have commits like this `1-> 2 -> 3 -> 4` and I just checked out a branch off of `4`. A) Are both my head and index pointing to `4`? B) And now if checkout out back to `2` is my `Head` at this moment `2` yet my index is `4`? C) I'm asking this because I'm doing both `git diff` and `git status` on a branch and the `git diff` doesn't include all the files that were changed while the `git status` does so I thought it might have something related to this... – mfaani Aug 06 '18 at 16:21
  • 1
    @Honey A/ yes. (read https://stackoverflow.com/a/45181361/6309 or https://stackoverflow.com/questions/39438168/git-checkout-ours-does-not-remove-files-from-unmerged-files-list to really know more about the index). B/ No, normally index and HEAD should be the same. The working tree might not though: https://stackoverflow.com/a/37204243/6309. `git checkout 2 -- .` would make the working tree identical to commit 2. – VonC Aug 06 '18 at 18:26
0

git diff is for showing the uncommitted changes (ie, the work that you have done but have not yet committed to the current project).

Here's a full explanation: http://git-scm.com/docs/git-diff

chimeracoder
  • 20,648
  • 21
  • 60
  • 60
  • 2
    Not exactly. VonC's answer is more accurate; `git diff` does *not* show changes that you've made, not yet committed to the project, but *have* added with a `git add`. –  Jan 26 '13 at 22:22