Playing with Git Diff
Generally, the git diff command is used to inspect changes we make when working on a Git repository. With the basic git diff command, Git will print all insertions and deletions we made in that repository per file affected by our work, for example:
$ git diff
diff --git a/puisi.md b/puisi.md
index 544438e..3274c94 100644
--- a/puisi.md
+++ b/puisi.md
@@ -4,4 +4,4 @@
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
Sed vitae faucibus ante
-Curabitur tempor eros a risus eleifend faucibus
+Integer sed urna ac ex porta congue
But did you know that git diff has many other uses that can help you analyze your repository more deeply? Here are some other uses of git diff that might help you in your day-to-day work.
Comparing References
We can provide references as input to git diff to find out the differences between two references. If we provide only one reference as a parameter to git diff, Git will compare the current working directory with the reference we provide.
$ git diff HEAD~
diff --git a/puisi.md b/puisi.md
index 0a96d0e..5ee54f9 100644
--- a/puisi.md
+++ b/puisi.md
@@ -1,6 +1,8 @@
# Puisi
+## oleh Lipsum
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
Sed vitae faucibus ante
-Curabitur tempor eros a risus eleifend faucibus
+Integer sed urna ac ex porta congue
+Etiam sagittis vehicula porta
If we provide two references, Git will compare the reference in the second position against the reference in the first position (order matters!).
Note: at the HEAD position, I added the line ## oleh Lipsum.
$ git diff HEAD~ HEAD
diff --git a/puisi.md b/puisi.md
index 0a96d0e..544438e 100644
--- a/puisi.md
+++ b/puisi.md
@@ -1,4 +1,5 @@
# Puisi
+## oleh Lipsum
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
If we reverse the order of the references:
$ git diff HEAD HEAD~
diff --git a/puisi.md b/puisi.md
index 544438e..0a96d0e 100644
--- a/puisi.md
+++ b/puisi.md
@@ -1,5 +1,4 @@
# Puisi
-## oleh Lipsum
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
These references can be provided as Git refs such as HEAD or tags, or they can be provided directly as a commit hash.
Printing Only the Names of Changed Files
Sometimes we only need to know which files have changed. We can achieve this by providing the --name-only flag to git diff.
$ git diff --name-only
puisi.md
Filtering File Changes by Status
Git can differentiate file changes in its working directory based on the type of change that occurred in those files. In git diff, we can filter changed files by type using the --diff-filter flag. Here is how to use it and the inputs we can provide to that flag.
--diff-filter=[(A|C|D|M|R|T|U|X|B)…[*]]
A: Added | added to index
C: Copied | copied
D: Deleted | deleted
M: Modified | modified
R: Renamed | renamed
T: Have their type changed | type changed
U: Unmerged | not yet merged
X: Unknown | unknown
B: Have had their pairing broken | changed up to a certain proportion
Suppose we have a working directory state like this:
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: aku-chairil-anwar.md
modified: puisi.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
hujan-bulan-juni-sapardi-djoko-darmono.md
no changes added to commit (use "git add" and/or "git commit -a")
We can run git diff only for deleted and modified files with the --diff-filter=DM option.
$ git diff --diff-filter=DM
diff --git a/aku-chairil-anwar.md b/aku-chairil-anwar.md
deleted file mode 100644
index fdcc0f3..0000000
--- a/aku-chairil-anwar.md
+++ /dev/null
@@ -1,22 +0,0 @@
-# Aku
-## oleh Chairil Anwar
-
-Kalau sampai waktuku
-Ku mau tak seorang kan merayu
-Tidak juga kau
-
-Tak perlu sedu sedan itu
-
-Aku ini binatan jalang
-Dari kumpulannnya terbuang
-
-Biar peluru menembus tubuhku
-Aku tetap meradang menerjang
-
-Luka dan bisa kubawa berlari
-Berlari
-
-Hingga hilang pedih peri
-
-Dan aku akan lebih tidak peduli
-Ku mau hidup seribu tahun lagi
diff --git a/puisi.md b/puisi.md
index 544438e..5ee54f9 100644
--- a/puisi.md
+++ b/puisi.md
@@ -4,4 +4,5 @@
Lorem ipsum dolor sit amet
Consectetur adipiscing elit
Sed vitae faucibus ante
-Curabitur tempor eros a risus eleifend faucibus
+Integer sed urna ac ex porta congue
+Etiam sagittis vehicula porta
Filtering File Changes to a Specific Path
There may be cases where we only need to focus on inspecting changes to files in a specific directory. We can instruct git diff to narrow its scope to a specific path by providing the path as a parameter.
Suppose we have a working directory state like this.
$ 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: puisi.md
modified: puisi/aku-chairil-anwar.md
modified: puisi/hujan-bulan-juni-sapardi-djoko-darmono.md
no changes added to commit (use "git add" and/or "git commit -a")
We can narrow the scope of git diff to only the puisi/ directory.
$ git diff puisi/
diff --git a/puisi/aku-chairil-anwar.md b/puisi/aku-chairil-anwar.md
index fdcc0f3..c8ba01b 100644
--- a/puisi/aku-chairil-anwar.md
+++ b/puisi/aku-chairil-anwar.md
@@ -1,5 +1,6 @@
# Aku
## oleh Chairil Anwar
+---
Kalau sampai waktuku
Ku mau tak seorang kan merayu
diff --git a/puisi/hujan-bulan-juni-sapardi-djoko-darmono.md b/puisi/hujan-bulan-juni-sapardi-djoko-darmono.md
index 38df39a..74c22c0 100644
--- a/puisi/hujan-bulan-juni-sapardi-djoko-darmono.md
+++ b/puisi/hujan-bulan-juni-sapardi-djoko-darmono.md
@@ -1,5 +1,6 @@
# Hujan Bulan Juni
## oleh Sapardi Djoko Darmono
+---
Tak ada yang lebih tabah
Dari hujan bulan Juni
However, it is recommended to use the shell expression -- to separate the options we provide to the git command from the path parameter we provide, as shown below.
$ git diff -- puisi/
Besides the cases above, there are of course many more uses of git diff to explore. Everything is documented on the Git SCM website. Let’s make a habit of reading documentation to boost our productivity!