Skip to content
Latchkey

git diff: Usage, Options & Common CI Errors

git diff shows exactly what changed between any two states of your repository.

Diff is the core inspection tool. --exit-code and --name-only make it a precise gate in pipelines.

What it does

git diff compares two sources - working tree vs index, index vs HEAD, or any two commits/branches - and prints the line-level differences.

Common usage

Terminal
git diff                      # working tree vs index
git diff --staged             # index vs HEAD (a.k.a. --cached)
git diff main..feature
git diff --name-only HEAD~1
git diff --quiet || echo "changes present"

Options

FlagWhat it does
--staged / --cachedCompare the index against HEAD
--name-onlyList changed file names only
--name-statusNames plus add/modify/delete status
--statSummary of changes per file
--exit-code / --quietExit non-zero if differences exist

Common errors in CI

A frequent gotcha: git diff alone ignores staged changes - use --staged to compare the index. With --exit-code, the command returns 1 when there are differences, which scripts may misread as failure; handle it deliberately (e.g. as a "needs formatting" signal).

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →