git diff: see exactly what changed
Line by line, before you commit. The red and the green.
git diff compares two versions of your files and prints only the lines that differ — minus for removed, plus for added.
01What diff shows
You changed a file three days ago and only vaguely remember what you did. git diff shows the difference between the files on your disk and their last staged or committed version — the work that exists but is not saved yet. Nothing else: an unchanged file produces no output at all.
The output is deliberately minimal. Lines starting with a minus were removed; lines starting with a plus were added. A line that was edited appears twice — once with a minus showing the old wording, once with a plus showing the new. Learning to read this instantly is one of those small skills that quietly compounds.
git diffA brand-new, never-staged file is invisible to git diff — there is no old version to compare against. Use status to see untracked files; use diff to see changes to files git already knows.
02Anatomy of the output
The header lines name the file twice — a/ is the old version, b/ is the new version; the letters are just git's naming habit. Then come hunks. Each hunk starts with an @@ line giving the line numbers it covers, followed by only the changed lines with a few unchanged context lines around them, so you can see where in the file you are.
The @@ numbers look cryptic but carry real information: -1,3 +1,3 means the hunk starts at line 1 of both versions and is 3 lines long in each. You can safely ignore the numbers at first — the minus and plus lines are what you actually read.
diff --git a/notes.txt b/notes.txt
--- a/notes.txt
+++ b/notes.txt
@@ -1 +1 @@
-old line
+new line03Two questions, two commands
Plain git diff answers: what changed on disk since the last time I staged anything? Its cousin git diff --staged answers: what is on the checklist versus the last photograph? Between the two of them — plus status for the summary — you can always tell three things apart: what is on disk, what is about to be photographed, and what is already saved.
A natural rhythm before every commit: git diff to review the actual lines you are about to save, then stage, then commit. Ten seconds of reading what you wrote beats an hour of untangling a mistake that shipped.
| command | answers the question |
|---|---|
| git diff | what changed on disk since staging? |
| git diff --staged | what will the next commit include? |
| git status | which files are in which area? |
Professionals read their diff before committing, every time. It catches secrets, debug leftovers, and accidental deletions while they are still one keystroke from permanent.
Practice
A committed file is waiting for an edit.
01.Change notes.txt to say something different.
hint: Try: echo "version two" > notes.txt
02.Run git diff and find your changed line.
hint: Type: git diff