50

If I run 'git status' on my repo it gives:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified: myfile

However, if I do a 'git diff myfile' it shows no differences. Is this because I have made a change and removed it so it is back to the original?

Should I run 'git checkout myfile' to clear it?

Noam
  • 2,945
  • 3
  • 16
  • 8

3 Answers3

73

Your file is already staged to be committed. You can show it's diff using the --cached option of git.

git diff --cached myfile

To unstage it, just do what git status suggests in it's output ;)

You can check The Git Index For more info.

Marcus Borkenhagen
  • 6,246
  • 1
  • 28
  • 32
  • 2
    This worked for me but only after I ran "git add" firstly. Thanks. – atw Feb 10 '15 at 14:56
  • 1
    exactly, he means you can run git diff yourFileName before staged your changes by running "git add" so it will works fine, but if you already staged your changes so you have to run "git diff --cached" – Mina Ragaie Jan 10 '19 at 02:15
13

I have a preference for the --staged alias, mostly because I find that --staged actually means what I want to do, i.e. show me the staged differences.

git diff --staged

The accepted answer is correct and I have no qualms with it. Just personally think that --cached feels like git diff is caching the answer and showing me pre-calculated results or something.

I also like git diff HEAD because it's a more general. It puts together two concepts that most folks know, that is:

  1. git diff <commit> allows you to see the difference between your current position and a previous commit.

  2. HEAD (or head because holding shift is annoying) is a reference to the tip of your branch. (For those of you who are counting keystrokes, @ is an alias for HEAD.)

Combined, these two generally useful concepts result in:

git diff head
git diff @
Liyan Chang
  • 6,801
  • 2
  • 34
  • 55
1

For Staging Area vs Repository comparison use

$git diff --staged

For Working vs Repository comparison use

$ git diff 

but if a file is changed and added to staging area ($ git add fileName) and we try to see the difference with ( $ git diff). It will not return any difference since the file is in staging area and it will not be compared with the repository.

Goyal Vicky
  • 871
  • 11
  • 13