git diff
can show you the differences between 2 commits. You can use git diff HEAD~1 HEAD -- <filename>
if the previous change is in the last commit.
But most of the time, the last change is not made in the last commit. It was made in n-th commits before. You can use git log -- <filename>
to find out the commit of the last change. This introduces an extra step. ๐
Actually, you can use git log -p
instead. Suppose we have a setup like below. We want to check the changes of b
.
git init
echo "a1" > a
echo "b1" > b
git add .
git commit -m "1st commit"
echo "b2" > b
git add b
git commit -m "2nd commit"
echo "a3" > a
git add a
git commit -m "3rd commit"
It doesn't show anything if we use git diff HEAD~1 HEAD -- b
, because it is comparing the 2nd and 3rd commit, there is no change in b
.
If we use git log -p -- b
, we can see all the changes on b
without knowing the commit hash.
commit 5bb2c7acf3ba237f7c3f2b367996e92dd10b271f
Author: Franz <xxx>
Date: Sat Sep 19 12:08:34 2020 +0800
2nd commit
diff --git a/b b/b
index c9c6af7..e6bfff5 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-b1
+b2
commit bd6508390bcb0f305a9e3e1f9a393d874df419c9
Author: Franz <xxx>
Date: Sat Sep 19 12:07:54 2020 +0800
1st commit
diff --git a/b b/b
new file mode 100644
index 0000000..c9c6af7
--- /dev/null
+++ b/b
@@ -0,0 +1 @@
+b1
If you want to see only the last change, you can use git log -p -1 -- b
.
One more thing, you can use --no-pager
if you don't like to switch to interactive mode when checking the diff. Just do git --no-pager log -p -- b
.
Top comments (1)
Love this. Thanks!