46

 Why is that git diff thinks there are no changes

..even if git status reports them as modified?

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   file-added
    modified:   file-with-changes   << it knows there are changes

but in order to see the difference, I need to explicitly add the last reversion hash..

$ git diff
  (nothing)

$ git diff rev-hash
diff --git a/file-with-changes b/file-with-changes
index d251979..a5fff1c 100644
--- a/file-with-changes
+++ b/file-with-changes
.
..
eridal
  • 1,288
  • 1
  • 11
  • 23

4 Answers4

72

Please try git diff --staged command.

Alternative options available are listed below.

git diff

shows changes between index/staging and working files. Since, in your case, git add moved your files-with-changes to staging area, there were no modifications shown/seen.

git diff --staged

shows changes between HEAD and index/staging. git diff --cached also does the same thing. staged and cached can be used interchangeably.

git diff HEAD

shows changes between HEAD and working files

git diff $commit $commit

shows changes between 2 commits

git diff origin

shows diff between HEAD & remote/origin

Bhaskar
  • 2,549
  • 1
  • 21
  • 23
5

git diff diffs against the index, not against your HEAD revision. By running git add, you've put the changes in your index, so of course there are no differences! Use

  • git diff HEAD to see the differences between your tree state and the HEAD revision, or
  • git diff --cached to see the differences between your index and the HEAD revision.
jub0bs
  • 60,866
  • 25
  • 183
  • 186
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

Ran into the exact same problem.

  • Add the new file that you created using git add filename1.c
  • Make another change in some other filename2.c that was already a part of the repository tracking system.
  • Do a git diff and you will only see the change to filename2.c show up. Changes to filename1.c will not show up.
  • However if you do a git status you will see the changes in both filename1.c and filename2.c show up.
  • Do a git commit -a -m "Changes to filename1.c and filename2.c blah blah"
  • Do a git push

You will see that filename1.c got committed.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Rishiaar
  • 31
  • 1
0

Because git diff by default checks differences between the staging area and your working copy. When you git add, your staging area matches your working copy and therefore diff reports no changes.

Adding the --cached flag tells diff to diff against HEAD.

Dave Zych
  • 21,581
  • 7
  • 51
  • 66