learn git.
0/25 done
learn git/everyday git/course 11 of 25

git diff: see exactly what changed

Line by line, before you commit. The red and the green.

7 min read·3 sections·2 tasks
the key idea

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.

[bash]· what is different right now
git diff
diff sees only tracked files

A 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.

[bash]· how one changed line looks
diff --git a/notes.txt b/notes.txt
--- a/notes.txt
+++ b/notes.txt
@@ -1 +1 @@
-old line
+new line

03Two 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.

commandanswers the question
git diffwhat changed on disk since staging?
git diff --stagedwhat will the next commit include?
git statuswhich files are in which area?
The pre-commit read-through

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.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks0/2 done
  1. 01.Change notes.txt to say something different.

    hint: Try: echo "version two" > notes.txt

  2. 02.Run git diff and find your changed line.

    hint: Type: git diff