Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff command documentation

Tags:

git

git-diff

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?

like image 396
Rahul Avatar asked Dec 05 '25 08:12

Rahul


1 Answers

  • 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.

like image 96
Deepesh Avatar answered Dec 07 '25 23:12

Deepesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!