33

We can see difference between repository and working directory with:

git diff 

We can see difference between repository and staging index with:

git diff --staged 

But how do we see difference between working directory and staging index?

3 Answers 3

58

Actually, git diff is between index and working tree. It just so happens that until you have staged changes to the index (with git add) that its contents will be identical to the HEAD commit.

git diff HEAD is between repo and working tree.

See 365git.tumblr.com post:

git diffs

Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand this because right now I have "file.txt" in repository with content "1". Then I modify that file in my working directory to have content "12" but WITHOUT adding it to staging index. Then when I enter "git diff" I DO get difference shown in console. So there is nothing in staging index but I still get the difference between repository and working directory. I am really confused now.
@matori82 by default, index is equal to repo. Index differs from repo only when you start adding file to it.
Ah... I didn't know that. Guess I will have to dive more into git internals to get information like this... Thank you.
However, the logic of this command seems to be even more complex. Eg. if I have one file in index and one modified file in working directory, when I do the "git diff" then difference is shown for the file in working directory but the difference for file in staging index is not shown at all! This is really really confusing now...
@matori82 That is expected: the goal of git diff is, by default,to show what has not yet been tracked (added to the index or committed). What you had to the index is no longer shown by default, because you already reviewed it and decided to make it part of the next commit.
|
13
  • git diff - Compare working area to index.
  • git diff --staged - Compare stage area to repository.
  • git diff HEAD - Compare working area to repository

To illustrate that, I changed a file with “Name Staged” text and than I added it (git add .). After that, I changed the file again, now I replaced the text to “Name Working Area” and than I run the follow commands:

enter image description here

Now you can see clearly how it works. Pretty cool, right?

Comments

1

Inspired by @VonC's accepted answer, instead of git diff HEAD you can also write:

git difftool --dir-diff --no-prompt HEAD 

This will open a directory diff showing the differences between your working tree and the repo. From there it's pretty easy to view changes to individual files (e.g. when using a tool like Beyond Compare with its tabbed UI).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.