Seeing What Changed - diff
git diff
git diff shows you the exact line-by-line changes between two states of your files - not just which files changed, but precisely what's different inside them. Which two states it compares depends entirely on the flags you pass:
| Command | Compares |
|---|---|
git diff | Working directory vs staging area (your unstaged edits) |
git diff --staged | Staging area vs the last commit (exactly what you're about to commit) |
git diff HEAD | Working directory vs the last commit (everything uncommitted, staged or not) |
git diff main feature | Two branches |
git diff abc123 def456 | Two specific commits |
git diff --stat | A short summary (files touched + line counts) instead of the full diff |
$ git diff --staged
diff --git a/app.py b/app.py
index 3a1b2c4..7f8e9d0 100644
--- a/app.py
+++ b/app.py
@@ -1,2 +1,2 @@
-print('hi')
+print('hello world')
Reading a diff: a line starting with - existed before and was removed; a line starting with + is new. The @@ -1,2 +1,2 @@ line (a "hunk header") just tells you which line numbers this chunk of changes covers in the old and new versions - you can usually skim past it and focus on the +/- lines themselves.
Tip: Always rungit diff --stagedright before committing - it's your last chance to catch a stray debugprint(), a hardcoded secret, or a file you didn't mean to include, before it becomes a permanent part of history.