1) Between working directory and staging area
2) Between working directory and last commit
3) Between staged area and last commit
4) Between working directory and a particular commit
5) Between staged area and a particular commit
6) Between two specified commits
Demo Example:
file1.txt
First line in file1.txt
Second line in file1.txt “file1.txt v1 is added”
file2.txt
First line in file2.txt
Second line in file2.txt
first commit: 2 files and each file contains 2 lines
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
file2.txt
First line in file2.txt
Second line in file2.txt
Third line in file2.txt
Fourth line in file2.txt
2nd commit : 2 files and each file contains 4 lines.
Now we are adding new line in file1.txt in working directory
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
We are adding file1.txt to staging area
git add file1.txt
Again we are adding a new line in file1.txt of working directory
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
sixth line in file1.txt
Case-1: To see the difference in File Content between Working Directory and staging Area
$ git diff file1.txt
Case-2: To see the difference in File Content between Working Directory and Last Commit
The last commit can be referenced by HEAD.
git diff HEAD file1.txt
It shows the differences between working copy and last commit copy.
$ git diff HEAD file1.txt
diff --git a/file1.txt b/file1.txt
Case-3: To see the difference in File Content between staged Copy and Last Commit
We have to use --staged option or --cached option.
git diff --staged HEAD file1.txt
It shows the differences between staged copy and last commit copy.
Here HEAD is optional. Hence the following 2 commands will produce same output
git diff --staged HEAD file1.txt
git diff --staged file1.txt
$ git diff --staged HEAD file1.txt
Case-4: To see the difference in File Content between specific Commit and Working Directory Copy
git diff 7chracters_of_specified_commitid filename
$ git log --oneline
6745461 (HEAD -> master) 2 files and each file contains 4 lines
e5705a6 2 files and each file contains 2 lines
Eg:
$ git diff e5705a6 file1.txt
diff --git a/file1.txt b/file1.txt
index d4effe0..e3e329f 100644
Case-5: To see the difference in file content between specific commit and staging area copy:
git diff --staged e5705a6 file1.txt
$ git diff --staged e5705a6 file1.txt
diff --git a/file1.txt b/file1.txt
index d4effe0..0e17c9d 100644
--- a/file1.txt
+++ b/file1.txt
Case-6: To see the difference in File Content between 2 specified Commits:
$ git log --oneline
6745461 (HEAD -> master) 2 files and each file contains 4 lines
e5705a6 2 files and each file contains 2 lines
$ git diff e5705a6 6745461 file1.txt
diff --git a/file1.txt b/file1.txt
index d4effe0..cadd0e1 100644
$ git diff 6745461 e5705a6 file1.txt
diff --git a/file1.txt b/file1.txt
index cadd0e1..d4effe0 100644
Undo Changes with git Checkout Command
$ git checkout -- file1.txt
It will discard any unstaged changes made in file1.txt.
After executing this command, staged copy content and working
directory content is same.
$ cat file1.txt
first line in file1.txt
second line in file1.txt
This is third line in file1.txt
This is fourth line in file1.txt
$ git diff file1.txt
$ git checkout -- file1.txt
$ cat file1.txt
first line in file1.txt
second line in file1.txt
Note: git checkout is applicable only for the files which are
already tracked by git. It is not applicable for new files.