How to See Changes in a Specific Git Commit
To see changes in a specific commit, use the
git show <commit-hash> command which displays the commit details and the changes made. Alternatively, git diff <commit-hash>^ <commit-hash> shows the differences introduced by that commit.Syntax
The main commands to see changes in a specific commit are:
git show <commit-hash>: Shows the commit message, author, date, and the changes made in that commit.git diff <commit-hash>^ <commit-hash>: Shows the difference between the commit and its parent, highlighting what was added or removed.
Here, <commit-hash> is the unique identifier of the commit you want to inspect.
bash
git show <commit-hash> git diff <commit-hash>^ <commit-hash>
Example
This example shows how to view changes in a commit with hash abc1234. It demonstrates both git show and git diff commands.
bash
git show abc1234 git diff abc1234^ abc1234
Output
commit abc1234
Author: Jane Doe <jane@example.com>
Date: Tue Apr 23 14:00 2024 +0000
Fix typo in README
diff --git a/README.md b/README.md
index e69de29..b7a8c3f 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-Hello Wrold
+Hello World
+Added a new line
Common Pitfalls
Common mistakes when viewing commit changes include:
- Using an incorrect or incomplete commit hash, which causes errors or no output.
- Forgetting to include the caret (
^) when usinggit diffto compare with the parent commit. - Trying to view changes in a merge commit without specifying parents, which can be confusing.
Always copy the full commit hash or use tab completion to avoid errors.
bash
git diff abc1234 # Wrong: This compares the commit to the working directory, not its parent git diff abc1234^ abc1234 # Right: Compares the commit to its parent to show changes introduced
Quick Reference
| Command | Description |
|---|---|
| git show | Displays commit details and changes made in that commit |
| git diff | Shows differences introduced by the commit compared to its parent |
| git log -p -1 | Shows the patch (changes) for a single commit |
| git diff | Alternative syntax to compare commit with its parent |
Key Takeaways
Use
git show <commit-hash> to see commit details and changes in one command.Use
git diff <commit-hash>^ <commit-hash> to view only the changes introduced by that commit.Always use the full or unique part of the commit hash to avoid errors.
Remember that
git diff <commit-hash> alone compares the commit to the working directory, not its parent.For merge commits, specify parent commits explicitly to see meaningful diffs.