3

I am having trouble understanding the explanation of git diff command given in the book linked at the git website.

According to the Pro Git Book, it says

That command( git diff) compares what is in your working directory with what is in your staging area.

.

From my working with git it seems as though git diff compares your working directory with your repository. I have a file that has been committed to the repo. I now modify this file. Without staging it, running git status shows me the file under Changes not staged for commit . Now if I run git diff I get the difference in code between what was in the repo and what is in the working directory. Shouldn't I get no output, as the modified file was not even staged?

Is there something wrong with my understanding in what the author was trying to convey?

Rahul
  • 177
  • 3
  • 9
  • 5
    The book author is correct, without additional parameters, `git diff` compares your working directory with your staging area. This means that if you make some changes, add those (so they are now in the staging area), then you make further changes and then you execute `git diff`, you will get back a diff between the two sets of changes. – Lasse V. Karlsen Jul 01 '19 at 07:58
  • in short, diff compares your change to staging area, be it remote repository (if changes are commited), local files (post-pre change), ets. This may help you: https://veerasundar.com/blog/2011/06/git-tutorial-comparing-files-with-diff/ as well as https://git-scm.com/docs/git-diff – Bart Jul 01 '19 at 07:58
  • 2
    @RahulKumar The index is not empty before you run `git add`. It's filled with encrypted contents equivalent to the commit you have checked out. – ElpieKay Jul 01 '19 at 08:28
  • @ElpieKay I understand now, thank you. – Rahul Jul 01 '19 at 08:33
  • 1
    FYI, this "git diff" graph is helpful. https://stackoverflow.com/questions/1587846/how-do-i-show-the-changes-which-have-been-staged/1587952#1587952 – Ssuching Yu Jul 01 '19 at 08:56
  • https://git-scm.com/docs/git-diff – Pavan Nagadiya Jul 01 '19 at 10:02

1 Answers1

1
  • git diff View difference between Stage and Working Directory
  • git diff --staged View difference between HEAD and Stage
  • git diff HEAD View difference between HEAD and Working Directory

Here Stage is the changes you have marked to include in the next commit.

Working Directory is the current directory you are working on and making changes.

HEAD is a reference to the last commit in the currently checked-out branch.

Try this once which might help you to clear up things:

Suppose I have a file test.txt and I have content Hello in the file which is currently in my repository. Now I change the file and add World and do git status:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

And when I check git diff it will show something like this:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World

Now if I stage this file and check git status:

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

    modified:   test.txt

Now I observe I forgot to add exclamation mark to the text so I add that and check git status again:

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

    modified:   test.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test.txt

So you can see that we have the same file in staged and unstaged area both. And when I check git diff it shows me this:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello World
+Hello World!

We have changed Hello World which is in staged area right now to Hello World!, so it compared to the staged area. And now if I check git diff --staged:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World

This compares the staged changes to the HEAD (the last commit). As I have not staged the ! change it is not showing it here. And finally when I will do git diff HEAD it will show this:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World!

The changes between HEAD (the last commit) and your Working Directory. As the HEAD has only Hello in the file and in your Working Directory you have changed it to Hello World! (it doesn't matters that you have not staged the ! change, it will just look the file for changes whether they are staged or unstaged).

Hope this helps.

Deepesh
  • 6,138
  • 1
  • 24
  • 41