Reputation: 490
According to the book Version Control with Git, "git diff shows the difference between your working directory and the index. It exposes what is dirty in your working directory and is thus a candidate to stage for your next commit."
What does "git diff shows the difference between your working directory and the index" mean? When you modify a file and run git diff, there's nothing in the staging area for git to compare the working directory to since you haven't staged anything yet so what is it comparing it to?
Upvotes: 0
Views: 52
Reputation: 391336
If your repository state is clean, meaning you have not yet modified any file, then the index matches the state of the repository at the commit HEAD points to.
In this case, if you modify a file and then do a git diff
, the modified file (in your working area) will be compared against the index, which will show the same differences as if you compared it against the current commit.
However, if you now stage that file (git add that-file
), then modify it again, and do another git diff
, your modified file (in your working area) will be compared against the staged and modified file in the index. This will now be different from comparing against the HEAD commit.
If you run these commands you will see that git diff
compares b with c, not a with c:
git init .
echo a >test.txt
git add .
git commit -m "Initial commit"
echo b >test.txt
git add .
echo c >test.txt
git diff
Output:
diff --git i/test.txt w/test.txt
index 2fea07c..12d6973 100644
--- i/test.txt
+++ w/test.txt
@@ -1 +1 @@
-b
+c
Upvotes: 2