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.