How can I view the changes made by a specific Git commit?
When I run git diff COMMIT, I see the differences between that commit and the current HEAD. However, I want to see only the changes that were introduced by that single commit itself. I’ve looked through the options for git diff and git log but haven’t found a straightforward way to achieve this.
The primary command to view changes made by a specific Git commit is git show [commit-hash], which displays the complete changes introduced by that single commit including the commit message, author information, and all file modifications. This command shows exactly what changes were introduced in that commit, not the differences between that commit and your current HEAD position.
Contents
- Using git show Command
- Alternative git diff Approaches
- Combining with git log for Enhanced View
- Filtering Changes by File or Directory
- Practical Examples and Use Cases
- Common Pitfalls and Solutions
Using git show Command
The git show command is the most straightforward way to view changes made by a specific commit. By default, it displays:
- The commit message and metadata
- The author and committer information
- A diff of all changes introduced in that commit
Basic Usage
git show [commit-hash]
Where [commit-hash] can be:
- The full commit hash (e.g.,
a1b2c3d4e5f6) - A shortened version (e.g.,
a1b2c3) - A relative reference (e.g.,
HEAD~3for 3 commits ago)
Key Options
--stat: Shows only the file statistics rather than the full diff--name-only: Lists only the files that were changed--oneline: Shows a one-line summary of the commit--color: Highlights the diff output (enabled by default)--no-pager: Prevents the output from being paginated
Example:
git show abc123 --stat
git show abc123 --name-only
git show abc123 --oneline
Alternative git diff Approaches
While git show is the most direct method, git diff can also be used with specific parameters to achieve the same result.
Using git diff with Parent Commit
To see changes made by a single commit, you need to compare that commit with its parent:
git diff [parent-hash]..[child-hash]
For a specific commit, this becomes:
git diff [commit-hash]^ [commit-hash]
Where ^ refers to the parent commit.
Using Three-Dot Notation
git diff [commit-hash]^! # Shorthand for the same operation
Practical Examples
# See changes in the last commit
git diff HEAD^ HEAD
# See changes in a specific commit
git diff abc123^ abc123
# Alternative syntax
git diff abc123^!
Combining with git log for Enhanced View
You can combine git log with git show or git diff to get more context about the commit you’re examining.
Using git log with show
git log -n 1 --show-notes=[notes-ref] [commit-hash]
This shows the commit information along with any associated notes.
Using git log with diff
git log -n 1 --stat [commit-hash]
This shows the commit with file change statistics.
Interactive Viewing
git log -p -n 1 [commit-hash]
The -p option shows the patch (diff) for each commit.
Filtering Changes by File or Directory
When working with large commits that modified many files, you often want to see changes only for specific files or directories.
Filtering by File
git show [commit-hash] -- [file-path]
Example:
git show abc123 -- src/main.js git show abc123 -- README.md
Filtering by Directory
git show [commit-hash] -- [directory-path]/
Example:
git show abc123 -- src/ git show abc123 -- docs/
Multiple Files
git show [commit-hash] -- [file1] [file2]
Example:
git show abc123 -- src/main.js src/utils.js
Practical Examples and Use Cases
Case 1: Understanding a Specific Commit
# Get the commit hash from git log first
git log --oneline
# Output: a1b2c3d (feat: Add user authentication)
# Then examine that specific commit
git show a1b2c3d
Case 2: Quick View of What Changed
# See only which files were changed
git show a1b2c3d --name-only
# See file statistics
git show a1b2c3d --stat
Case 3: Checking Changes in a Branch
# When you want to see what changed in a branch compared to master
git show origin/feature-branch --stat
Case 4: Visualizing Changes
# Use gitk for visual representation
gitk [commit-hash]
# For text-based visualization
git show [commit-hash] --color | less -R
Common Pitfalls and Solutions
Problem 1: Long Commit Hash
If you find typing long commit hashes tedious, use tab completion:
git show a1b2<TAB> # Git will complete the hash
Problem 2: Ambiguous References
When you have multiple commits with similar short hashes, use more characters:
# Instead of
git show abc12 # Might be ambiguous
# Use
git show abc1234 # More specific
Problem 3: Binary Files
For commits that modified binary files, use:
git show [commit-hash] -- [binary-file]
Problem 4: Large Output
The output can be overwhelming for large commits. Use these strategies:
# View only file names first
git show [commit-hash] --name-only
# Then examine specific files
git show [commit-hash] -- [specific-file]
Problem 5: Merge Commits
For merge commits, you might want to see the merge changes:
git show [merge-commit] --cc
Sources
- Official Git Documentation - git show
- Official Git Documentation - git diff
- Official Git Documentation - git log
- Pro Git Book - Viewing the Commit History
- Git Reference - git show
Conclusion
To view changes made by a specific Git commit, the most effective approach is using git show [commit-hash] which displays exactly what changes were introduced in that single commit. For more precise control, you can combine it with options like --stat for file statistics or --name-only to see only affected files. Alternative methods include using git diff [commit-hash]^ [commit-hash] to compare a commit with its parent, or filtering the output to specific files using git show [commit-hash] -- [file-path]. These commands provide comprehensive ways to understand the exact changes introduced by any commit in your Git history.