Comparing Between Commits
You'll learn how to use git diff to compare any two snapshots in your project's history, helping you understand exactly what changed between versions.
The Core Idea
So far, you've used git diff to compare your current work against recent states. But what if you need to see what changed between any two points in your repository's history? Maybe you want to compare last week's model code to last month's, or see differences between two experimental branches.
git diff accepts commit hashes or branch names as arguments, letting you compare any two commits directly—like placing two snapshots side by side.
Comparing Two Specific Commits
Use commit hashes (from git log) to compare:
git diff abc123 def456
This shows what changed from commit abc123 to commit def456. The order matters—it's like asking "what would I need to do to transform abc123 into def456?"
You can use shortened hashes (first 7 characters usually work):
git diff a1b2c3d e4f5g6h
Comparing Branches
Compare your current branch to another:
git diff main feature-branch
This shows what's different between the two branches, helping you review changes before merging.
Practical Use
When debugging, you might discover a bug that wasn't present two weeks ago. Find the commit hash from that time using git log, then compare:
git diff f8e7d6c HEAD model/training.py
This isolates changes to just model/training.py between that old commit and your current state (HEAD).
Key Takeaway: git diff with commit hashes or branch names lets you compare any two points in your repository's timeline, making it easy to track down when and what changed across your project's evolution.