Viewing Changes for a Specific File in a Commit
Sometimes, a commit might change multiple files, but you are only interested in the changes to one specific file. You can tell git show to only display the changes for a particular file by adding the file path to the end of the command.
Let's look at the most recent commit, which has the message "Add application file". This commit added the app.py file.
First, get the hash for the latest commit from your git log --oneline output. Then, run git show with that hash, followed by -- and the filename app.py.
git show app.py < latest-commit-hash > --
For example, if the latest commit hash is a1b2c3d, the command would be:
git show a1b2c3d -- app.py
The output will now be limited to the changes made to app.py in that commit.
commit a1b2c3d...
Author: LabEx <labex@example.com>
Date: ...
Add application file
diff --git a/app.py b/app.py
new file mode 100644
index 0000000..d95f32b
--- /dev/null
+++ b/app.py
@@ -0,0 +1 @@
+print("Hello, Git!")
Notice that the output is much shorter and only contains the diff for app.py. The line new file mode 100644 indicates that this file was created in this commit.