Viewing Changes with git diff
What is a Diff?
A diff shows the exact lines that changed between two versions of a file. Lines added appear in green with a +, and lines removed appear in red with a -.
Using git diff
# See unstaged changes (working dir vs staging)
git diffSee staged changes (staging vs last commit)
git diff --stagedSee changes for a specific file
git diff filename.txt
Reading a Diff
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1,3 @@
Hello, World!
-This is the old line.
+This is the new line.
+Added this brand new line.
Breaking it down:
--- a/hello.txt= the old version+++ b/hello.txt= the new version- Lines starting with
-were removed - Lines starting with
+were added - Lines with no prefix are unchanged (context)
Comparing Commits
# Compare two specific commits
git diff abc123 def456Compare with the previous commit
git diff HEAD~1
Summary
| Command | Shows |
|---|---|
| <code>git diff</code> | Unstaged changes |
| <code>git diff --staged</code> | Staged changes |
| <code>git diff HEAD~1</code> | Changes since last commit |
💡 Tip: Reading diffs is an essential skill. On GitHub, every Pull Request shows diffs for code review.