How do I view previous diff commits using Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git provides several commands to view diffs between commits. git log -p shows the patch (diff) for each commit. git diff <commit1> <commit2> compares two specific commits. git show <commit> displays a single commit's changes. These commands let you inspect what changed, when, and by whom — essential for debugging, code review, and understanding project history.
View Diff for Each Commit (git log -p)
git log -p combines the commit message with the full patch output. Press q to exit the pager.
View a Single Commit's Changes (git show)
Compare Two Commits (git diff)
Diff with Statistics
View Commits Between Two Points
Search Commit Diffs
Viewing Diffs with Context
Using gitk and Other Viewers
Useful Aliases
Common Pitfalls
- Confusing
..and...range operators:git log A..Bshows commits in B but not in A.git log A...Bshows commits in either but not both. Using the wrong operator shows unexpected commit ranges. - Forgetting
--before file paths:git diff abc123 src/app.jstries to diff between commitabc123and a ref calledsrc/app.js. Usegit diff abc123 -- src/app.jsto specify a file path explicitly. - Large diffs overwhelming the terminal:
git log -pon a long history produces enormous output. Always limit with-n 5,--since, or filter by file path. Or pipe tolessfor paging. - Not using
--statfor quick overviews: Reading full diffs to understand what changed is slow. Start withgit diff --statorgit log --statfor a summary, then drill into specific files. - Assuming
git diffshows staged changes:git diffwithout arguments shows only unstaged changes. Usegit diff --staged(or--cached) to see what is staged for the next commit.
Summary
git log -pshows the patch for each commit — add-n 5to limit outputgit show <commit>displays a single commit's diff and metadatagit diff <commit1> <commit2>compares any two commits- Use
--statfor a quick summary and--name-onlyfor just file names - Use
-S "text"to find commits that added or removed specific strings git diffshows unstaged changes;git diff --stagedshows staged changes
Related reading
- How do I work with a git repository within another repository?
- How do you attach a new pull request to an existing issue on github?
- How do you change the capitalization of filenames in Git?
- How do you copy and paste into Git Bash
- How do you fix a bad merge, and replay your good commits onto a fixed merge?
- How do you get git to always pull from a specific branch?
- How do you merge two Git repositories?
- How do you organise multiple git repositories, so that all of them are backed up together?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.