git
version control
diff
commit history
git commands

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.

Browse interview questions

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)

bash
1# Show full diff for every commit
2git log -p
3
4# Limit to last 3 commits
5git log -p -3
6
7# Show diffs for a specific file
8git log -p -- src/app.js
9
10# Show diffs with stats summary
11git log -p --stat

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)

bash
1# Show the latest commit
2git show
3
4# Show a specific commit by hash
5git show a1b2c3d
6
7# Show only the files changed (no diff content)
8git show --stat a1b2c3d
9
10# Show a specific file's change in that commit
11git show a1b2c3d -- src/app.js
12
13# Show the diff in a specific format
14git show a1b2c3d --format=oneline

Compare Two Commits (git diff)

bash
1# Diff between two commits
2git diff abc123 def456
3
4# Diff between a commit and HEAD
5git diff abc123 HEAD
6
7# Diff between a commit and its parent
8git diff abc123^..abc123
9# Or equivalently:
10git show abc123
11
12# Diff for a specific file between two commits
13git diff abc123 def456 -- src/app.js
14
15# Only show file names that changed
16git diff --name-only abc123 def456
17
18# Show file names with change type (Added, Modified, Deleted)
19git diff --name-status abc123 def456

Diff with Statistics

bash
1# Summary of changes (insertions/deletions per file)
2git diff --stat abc123 def456
3
4# Short stat (total files, insertions, deletions)
5git diff --shortstat abc123 def456
6
7# Example output:
8# 5 files changed, 120 insertions(+), 45 deletions(-)

View Commits Between Two Points

bash
1# All commits between two branches
2git log main..feature-branch
3
4# With diffs
5git log -p main..feature-branch
6
7# All commits on feature-branch not in main (same as above)
8git log feature-branch --not main
9
10# Commits in either branch but not both
11git log main...feature-branch

Search Commit Diffs

bash
1# Find commits where "TODO" was added or removed
2git log -p -S "TODO"
3
4# Find commits where a regex pattern changed
5git log -p -G "function\s+handleClick"
6
7# Find commits by author with diffs
8git log -p --author="Alice"
9
10# Find commits in a date range
11git log -p --since="2024-01-01" --until="2024-02-01"

Viewing Diffs with Context

bash
1# Show more context around changes (default is 3 lines)
2git diff -U10 abc123 def456  # 10 lines of context
3
4# Show word-level diff instead of line-level
5git diff --word-diff abc123 def456
6
7# Color-coded word diff
8git diff --word-diff=color abc123 def456
9
10# Show moved lines (Git 2.17+)
11git diff --color-moved abc123 def456

Using gitk and Other Viewers

bash
1# GUI commit browser with diffs
2gitk --all
3
4# TUI (terminal) viewer
5tig
6
7# VS Code diff viewer
8git difftool abc123 def456

Useful Aliases

bash
1# Add to ~/.gitconfig
2git config --global alias.lg "log --oneline --graph --all"
3git config --global alias.last "log -1 -p"
4git config --global alias.changes "diff --name-status"
5git config --global alias.review "log -p --reverse"
6
7# Usage
8git lg                    # Compact graph view
9git last                  # Diff of the latest commit
10git changes HEAD~3 HEAD   # Files changed in last 3 commits
11git review main..HEAD     # Review all changes since branching from main

Common Pitfalls

  • Confusing .. and ... range operators: git log A..B shows commits in B but not in A. git log A...B shows commits in either but not both. Using the wrong operator shows unexpected commit ranges.
  • Forgetting -- before file paths: git diff abc123 src/app.js tries to diff between commit abc123 and a ref called src/app.js. Use git diff abc123 -- src/app.js to specify a file path explicitly.
  • Large diffs overwhelming the terminal: git log -p on a long history produces enormous output. Always limit with -n 5, --since, or filter by file path. Or pipe to less for paging.
  • Not using --stat for quick overviews: Reading full diffs to understand what changed is slow. Start with git diff --stat or git log --stat for a summary, then drill into specific files.
  • Assuming git diff shows staged changes: git diff without arguments shows only unstaged changes. Use git diff --staged (or --cached) to see what is staged for the next commit.

Summary

  • git log -p shows the patch for each commit — add -n 5 to limit output
  • git show <commit> displays a single commit's diff and metadata
  • git diff <commit1> <commit2> compares any two commits
  • Use --stat for a quick summary and --name-only for just file names
  • Use -S "text" to find commits that added or removed specific strings
  • git diff shows unstaged changes; git diff --staged shows staged changes

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.