4 Exploring History (git diff & show)

4.1 Difference between current file and N commit ago (git diff HEAD)

Let’s add another line to foo.txt

echo world >> foo.txt

Check what is different from your current version of foo.txt compared to the last commit

git diff HEAD foo.txt
## diff --git a/foo.txt b/foo.txt
## index ce01362..94954ab 100644
## --- a/foo.txt
## +++ b/foo.txt
## @@ -1 +1,2 @@
##  hello
## +world

You see that “world” is preceded by a plus sign, indicating it is an insertion.

Check difference compared to two commits ago

git diff HEAD~1 foo.txt
## diff --git a/foo.txt b/foo.txt
## index e69de29..94954ab 100644
## --- a/foo.txt
## +++ b/foo.txt
## @@ -0,0 +1,2 @@
## +hello
## +world

You can check difference between the current file against a specific commit based on its identifier (the unique identifier is different for you, use git log to check!)

git diff <unique identifier> foo.txt

4.2 What was done in ____ commit? (git show)

Sometimes we want to check what was done during a certain commit (instead of comparing differences)

git show HEAD~1 foo.txt
## commit 497ae8c350bd3a4bbb830fd14f3d72f468cbaea1
## Author: Shady Whale <shadywhale@allthewhales.com>
## Date:   Fri Oct 29 14:24:30 2021 +0000
## 
##     Add foo.txt to repo
## 
## diff --git a/foo.txt b/foo.txt
## new file mode 100644
## index 0000000..e69de29

You have now compared different versions of a file using git diff and check its changes during a specific commit using git show.