git diff
Compare any two versions of your content.
git diffWhat it does
git diff highlights every difference between two states: working tree vs index, index vs HEAD, or any two arbitrary commits. It's the same engine that colors pull requests red and green. Read-only — it never changes a thing.
Two blob versions, reduced to an edit script
Watch it — press play
A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.
Step 1
What did I touch?
git diff compares your working files against the index — every edit you haven't staged yet.
git diffWorktree vs index = the red and green you just made
Under the hood
Blob-to-blob, using their SHA-1 hashes
Git compares content by hashing. If both blobs are identical, the hash matches and the file is skipped instantly. Only differing pairs enter the diff algorithm.
The Myers diff algorithm
Diffing is finding the shortest edit script between two sequences. Git uses the Myers diff (in the xdiff library): it hunts the minimal set of insertions/deletions to turn one blob into the other, capably finding moved-but-identical lines.
Hunks, not files
The output is grouped into hunks — contiguous changed regions — prefixed with @@ -old +new @@ line numbers. This is the exact format git apply consumes, which is how patches become commits.
The three comparison modes
Plain `git diff` compares the working tree against the index. `git diff --cached` compares the index against HEAD (what you've staged). `git diff A B` compares any two revisions.
Aliases the pros type
Memorize the git diff concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:
git config --global alias.st # → git diffg dgit diffCheck unstaged changes.
g dcgit diff --cachedStaged changes — what's about to be committed.
g dsgit diff --statFile-level summary without the noise.
g difwgit diff --word-diffRobust for prose and docs editing.
Command anatomy
--cachedDiff the index against HEAD — what your next commit will do.
--statJust which files change, as a compact bar chart.
--name-onlyOnly the changed file names, nothing else.
--word-diffHighlight changes word-by-word instead of line-by-line.
When to reach for it
Reviewing every edit you've made before staging: git diff.
Double-checking exactly what a commit changed: git diff HEAD~1 HEAD.
A quick sanity diff between two branches: git diff main..feature.
Pro tip
Chain aliases for a standing review ceremony: `git config --global alias.review '!git diff; git diff --cached'` then `git review` shows both unstaged and staged work before every commit.