1

As per my understanding git diff is used to compare files between Working Directory and Staging Area. However, If I have no staged changes in the Staging Area, the same command git diff compare files between Working Directory and HEAD.

Why the same command (git diff) is giving two different outputs?

How do I actually diff between Working Directory and Staging Area?

5

1 Answer 1

5

First, one point needs clarification :

In git, when one talks about files in "the staging area" or "the index", they actually refer to :

  • the files as they are in the HEAD commit,
  • plus the potential git added modifications.

So the meaning of "there are no modifications in the staging area" is :
"the staging area is the same as HEAD".


With that in mind, you can re-read the diagram posted by VonC :

  • git diff will display the differences between the worktree and the staging area (the index) ;
  • when there are no extra modifications staged, "the index" is the same as HEAD -- as a consequence, git diff will indeed show the difference between HEAD and your worktree ;
  • the command that will always show you "the difference between HEAD and your worktree" (and disregard the index) is : git diff HEAD
Sign up to request clarification or add additional context in comments.

2 Comments

I just found that If you have no staged changes, git diff will show Working Directory Vs HEAD. Please let me know if you find something different.
@blueray : I think one point needs to be clarified : when one talks about "the index" or "staged files" in git, it actually means : the files as they are in the HEAD commit, plus the modifications that have been git added. So "no modifications staged" is the same as "the index is equal to HEAD". Does this shed some new light on the diagram you linked ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.