All commands

git diff

Compare any two versions of your content.

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

@@ -1,3 +1,3 @@ src/App.tsxMyers diff → edit script
const header = 'OLD'
const header = 'NEW'
const nav = [ 'home', 'about' ]
const nav = [ 'home', 'about', 'blog' ]
return <div>
return <div>

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.

1 / 3

Step 1

What did I touch?

git diff compares your working files against the index — every edit you haven't staged yet.

git diff
@@ -1,3 +1,3 @@ src/App.tsxMyers diff → edit script
const header = 'OLD'
const header = 'NEW'
const nav = [ 'home', 'about' ]
const nav = [ 'home', 'about', 'blog' ]
return <div>
return <div>

Worktree vs index = the red and green you just made

Under the hood

1

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.

2

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.

3

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.

4

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 diff
AliasWhy people use it
g d

Check unstaged changes.

g dc

Staged changes — what's about to be committed.

g ds

File-level summary without the noise.

g difw

Robust for prose and docs editing.

Command anatomy

git diff [<revA>] [<revB>]
--cached

Diff the index against HEAD — what your next commit will do.

--stat

Just which files change, as a compact bar chart.

--name-only

Only the changed file names, nothing else.

--word-diff

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

Go deeper with