git diff & git show — Comparing Changes
Module 01 30 min Coming Soon
Section Objectives
- Compare working directory vs staging vs last commit with
git diff - Inspect a specific commit's contents with
git show - Compare branches and tags
- Read unified diff output
What is git diff?
git diff shows differences between two states of your repository:
| Command | Compares |
|---|---|
git diff | Working directory vs staging area |
git diff --staged | Staging area vs last commit |
git diff HEAD | Working directory vs last commit |
git diff main..feature | Two branches |
git diff abc123 def456 | Two specific commits |
git diff README.md
git diff --staged
git diff HEAD~1 HEAD
What is git show?
git show displays the contents of a single Git object (usually a commit):
git show # Show last commit (HEAD)
git show abc123 # Show specific commit by SHA
git show HEAD~2 # Show 2 commits ago
git show v1.0.0 # Show what a tag points to
Reading Unified Diff Output
diff --git a/file.txt b/file.txt
index 0a3b1e2..b7c4f5d 100644
--- a/file.txt
+++ b/file.txt
@@ -1,4 +1,5 @@
Hello
-World
+World!
+New line added
End
---= old version,+++= new version@@ -1,4 +1,5 @@= hunk header (line numbers)-= removed line,+= added line,= unchanged context
Useful Flags
git diff --stat # Summary of changes per file
git diff --name-only # Just list changed files
git diff --color-words # Highlight word-level diffs
git diff -- path/to/file # Limit to one file/directory
git show --stat HEAD # Stats for last commit
git show --name-only HEAD # Files changed in last commit
Next Steps
info
This lesson page is a stub. Detailed examples and exercises will be added in a future revision.