Git Diff: Compare Changes Across Commits and Branches
What Git Diff Shows
git diff shows the differences between two states of your repository — unstaged changes, staged changes, commits, or branches.
# Unstaged changes (working directory vs staging) git diff # Staged changes (staging vs last commit) git diff --staged # All changes (unstaged + staged) git diff HEAD
Comparing Commits
# Between two commits git diff abc1234..def5678 # Between a commit and HEAD git diff HEAD~3..HEAD # Just the files that changed (no content) git diff --name-only HEAD~3..HEAD
Comparing Branches
# What's different between main and feature git diff main..feature # What feature has that main doesn't: git diff main...feature # (three dots = changes on feature since it diverged from main)
Reading Diff Output
diff --git a/src/app.ts b/src/app.ts --- a/src/app.ts +++ b/src/app.ts @@ -10,6 +10,8 @@ const port = 3000; +const host = "0.0.0.0"; app.listen(port);
Red lines with - are removed, green lines with + are added. The @@ line shows the file position.
Useful Diff Options
# Ignore whitespace git diff -w # Show word-level changes (not line-level) git diff --word-diff # Show stats only git diff --stat # Generate a patch file git diff > changes.patch
Diff in Review Workflows
Use git diff before every commit to catch unintended changes. It is the fastest code review tool you have — it runs locally, shows exactly what you are about to commit, and takes seconds.
Master Git log, diff, and blame offline
Read advanced graph traversals, patch analysis, and investigation workflows in the 97-page offline course — ₹499.
Related Git Command References
All 28 commands →Syntax breakdowns and practical terminal examples for commands used in this topic
More on History & Diff Inspection
All articles →- Tutorial
Git Log Formats: Customize Your History Output
Master git log with custom formats, filters, and graph views. Learn the format placeholders that turn raw history into a readable, useful log.
Read guide - Tutorial
Git Blame: Track Changes Back to Their Author
Use git blame to find who last modified every line of a file. Essential for code review, debugging, and understanding project history.
Read guide