Git diff shows changes, and which changes depends on the arguments. With no arguments it compares your working tree to the index, so it shows edits you have made but not yet staged. git diff --cached (or --staged) flips that: it compares the index to the last commit, which is what a plain git commit would record.[1] Add a commit name and the working tree is compared to that commit instead. Name two commits and you compare those two. Below: the comparison forms in a table, how to read the patch output, the flags that matter, edge cases around renames, binaries and merges, and the mistakes that waste the most time. To compare two pasted snippets with no repository at all, the diff checker does it in the browser.
What git diff does
The official description is broad: show changes between the working tree and the index or a tree, between the index and a tree, between two trees, from a merge, between two blob objects, or between two files on disk.[1] Every one of those is the same operation. Git picks two endpoints, compares them, and prints what differs.
Three places hold your content, and the whole command makes sense once you hold them in mind. The working tree is the files on disk. The index (the staging area) is what the next commit will contain. HEAD is the last commit. Each form of git diff is a pair chosen from those, or from other commits you name.
Staged versus unstaged: the four everyday forms
The docs list four ways to check your working tree, and they cover most daily use.[1]
git diff # (1) working tree vs index: not yet staged
git diff --cached # (2) index vs last commit: what git commit would record
git diff HEAD # (3) working tree vs last commit: what commit -a would record
git diff AUTO_MERGE # (4) working tree vs the auto-merge result, during conflicts
Form 1 is "changes in the working tree not yet staged for the next commit." Form 2 is "what you would be committing if you run git commit without -a option." Form 3 is "what you would be committing if you run git commit -a."[1] Form 4 uses the AUTO_MERGE ref, which the ort merge strategy writes when it hits conflicts. Comparing against it shows the changes you have made so far to resolve textual conflicts.[1]
Comparison forms at a glance
The classic confusion: stage a file with git add, run plain git diff, and the output is empty, because the change moved to the --cached view.
| Command | Compares | Use it for |
|---|
| git diff | Working tree vs index[1] | Review edits before staging |
| git diff --cached | Index vs HEAD; --staged is a synonym[1] | Review exactly what you are about to commit |
| git diff HEAD | Working tree vs latest commit[1] | See staged and unstaged together |
| git diff <commit> | Working tree vs the named commit; a branch name compares with that branch's tip[1] | How far your files have drifted from another branch |
| git diff A B | Two arbitrary commits[1] | Compare two tags, releases or branch tips |
| git diff A...B | Equivalent to git diff $(git merge-base A B) B[1] | What a branch added since it split off |
| git diff --no-index a b | Two paths on the filesystem; implies --exit-code[1] | Diff files that are not tracked by Git |
Comparing commits and branches
The docs give a compact set of examples for comparing against arbitrary commits and between branches.[1]
git diff test # working tree vs the tip of branch "test"
git diff HEAD -- ./test # working tree vs HEAD, limited to the file "test"
git diff HEAD^ HEAD # the version before the last commit vs the last commit
git diff topic master # changes between the two branch tips
git diff topic..master # same as above
git diff topic...master # changes on master since topic was started off it
Here is the part that trips people up. The two-dot and three-dot spellings in git diff do not mean what they mean in git log. The docs spell it out: diff is about comparing two endpoints, not ranges, and the range notations do not mean a range as defined in the "SPECIFYING RANGES" section of gitrevisions.[1] Two dots is just "diff these two tips." Three dots starts from the common ancestor, which makes it the right choice for "what did this feature branch change," because it ignores whatever landed on the other branch in the meantime.
Reading the patch output
The default output is patch text. -p, -u and --patch all request it, and it is the default.[1] Each changed file starts with a header that differs slightly from a traditional diff.
diff --git a/file1 b/file2
old mode
new mode
deleted file mode
new file mode
copy from
copy to
rename from
rename to
similarity index
dissimilarity index
index ..
That block is the header form from the docs, not output from a real run: the first line is always present, and one or more of the extended lines follow it depending on what happened to the file.[1] Reading it:
The
a/andb/names are the same unless a rename or copy is involved. Even for a creation or deletion,/dev/nullis not used in place of them.[1]File modes print as 6-digit octal numbers that include the file type and permission bits. The mode appears on the
indexline only when it does not change; otherwise separateold modeandnew modelines show it.[1]The similarity index is the percentage of unchanged lines, and the dissimilarity index is the percentage of changed lines, both rounded down. A similarity of 100% is reserved for two equal files.[1]
Lines in the hunk body start with
+for new,-for old and a space for context.[1] Hunk headers also name the function the hunk applies to, and how that name is found can be customized per language through gitattributes.[1]
The flags worth knowing
| Flag | What it does |
|---|
| --stat | A diffstat. Width defaults to terminal width, or 80 columns when not connected to a terminal[1] |
| --numstat | Added and deleted counts in decimal with full pathnames, for scripts. Binary files print two - instead of 0 0[1] |
| --name-only | Only the name of each changed file in the post-image tree[1] |
| --name-status | Name plus a status letter for each changed file[1] |
| -U<n> | Use n lines of context. Defaults to diff.context or 3[1] |
| -w | Ignore whitespace when comparing lines, even if one line has whitespace where the other has none[1] |
| --word-diff | Word-level changes inside each hunk; mode defaults to plain[1] |
| --color-moved | Color moved lines differently; defaults to zebra when given with no mode[1] |
| --diff-filter= | Keep only files by status: Added, Copied, Deleted, Modified, Renamed, Type changed, Unmerged. Lowercase letters exclude[1] |
| -M / -C | Detect renames, or copies as well[1] |
| -S<string> / -G<regex> | Find changes that alter the count of a string, or whose added or removed lines match a regex[1] |
| -R | Swap the two inputs, so the diff runs in reverse[1] |
| --check | Warn about conflict markers or whitespace errors; exits non-zero if found[1] |
| --exit-code / --quiet | Exit 1 when there are differences, 0 when none. --quiet also suppresses all output[1] |
Limiting the output
A big diff is rarely useful as raw patch text. The docs show three ways to cut it down: git diff --diff-filter=MRC (modification, rename and copy, no add or delete), git diff --name-status (names and the nature of change, no diff body) and git diff arch/i386 include/asm-i386 (limit output to named subtrees).[1] A directory name after the options gives every file under it, and --relative excludes changes outside the directory you are in.[1] The status letters are A (addition), C (copy), D (deletion), M (modification of contents or mode), R (rename), T (type change), U (unmerged) and X (unknown, which the docs call "most probably a bug").[1]
Whitespace noise and word-level diffs
A reformat that touches every line buries the real change, and the whitespace flags hide it.[1]
git diff -w # ignore all whitespace when comparing lines
git diff -b # ignore changes in the amount of whitespace
git diff --ignore-space-at-eol # ignore trailing whitespace changes only
git diff --word-diff # [-removed-] and {added} inline
git diff --word-diff=color # color only, no delimiters
git diff --word-diff-regex=. # character-by-character differences
git diff --check # flag whitespace errors and conflict markers
Plain word diff shows words as [-removed-] and {added}, and the docs warn that it makes no attempt to escape the delimiters if they appear in the input, "so the output may be ambiguous."[1] By default a word is a run of non-whitespace, and --word-diff-regex=. treats each character as a word. The docs also caution that --word-diff works by taking the same line-by-line diff and computing word changes inside each hunk, so it can produce a larger diff than a dedicated word-diff tool.[1]
For whitespace hygiene before a commit, --check is the better tool than eyeballing. By default it treats trailing whitespace, including lines that are only whitespace, and a space immediately followed by a tab inside the initial indent as errors; core.whitespace changes that.[1] When a whitespace-only diff is the question, pasting both versions into the diff checker shows the result without touching your repository state.
Using git diff in scripts and CI
Exit status is the interface for automation. --exit-code makes the command exit with codes similar to diff(1): 1 if there were differences and 0 for none. --quiet disables all output and implies --exit-code.[1]
# fail a script step if tracked files have unstaged changes
git diff --quiet || echo "working tree has unstaged changes"
# fail if the index differs from HEAD
git diff --cached --quiet || echo "staged changes present"
# machine-readable per-file counts (tab separated: added, deleted, path)
git diff --numstat
One caveat: --check exits non-zero on problems but documented as not compatible with --exit-code, so use one or the other per command.[1] For --numstat or --name-only output a script will parse, add -z, which uses NULs as terminators and stops pathnames being quoted per core.quotePath.[1]
Edge cases
Renames and copies. Rename detection is on by default for the porcelain git diff: diff.renames defaults to true, and setting it to copies also detects copies.[1] The similarity threshold defaults to 50%, and -M100% limits detection to exact renames.[1] The exhaustive fallback is O(N^2), so it is skipped past a file-count limit: -l defaults to diff.renameLimit, which is currently 1000 when unset.[1] A huge refactor may therefore show as separate deletes and adds. --no-renames turns detection off entirely.[1]
Binary files. --numstat prints two - for them, and -G ignores binary patches unless --text is supplied. --binary outputs a binary diff that git apply can apply.[1]
Merge conflicts. During a conflict, -1 (--base), -2 (--ours) and -3 (--theirs) compare the working tree against stage 1, 2 or 3 of the index, which exists only for unmerged entries. -0 omits diff output for unmerged entries and just prints "Unmerged".[1] For a merge commit itself, git diff A A^@, git diff A^! and git show A all give the same combined diff.[1] Combined diffs use diff --cc or diff --combined headers and one column per parent. Their chunk headers carry one more @ than the number of parents, which keeps people from feeding them to patch -p1 by accident.[1]
The diff algorithm. The default is myers, "the basic greedy diff algorithm." patience, histogram and minimal are alternatives, chosen with --diff-algorithm= or diff.algorithm. To return to the default after configuring another, pass --diff-algorithm=default.[1]
Config that changes what you see
When two machines print different diffs for the same change, check configuration. diff.context replaces the default of 3 context lines (and -U overrides it), diff.algorithm picks the algorithm, diff.colorMoved colors moved lines, diff.renames takes false, true (the default) or copies, and diff.mnemonicPrefix swaps the a/ and b/ prefixes.[1] Several have command-line counterparts that override them: --no-color-moved, --no-relative, --no-renames and --default-prefix.[1] Run once with the overrides before blaming the repository.
Common mistakes
Running plain git diff after git add and reading the empty output as "no changes." The change is in the index. Use git diff --cached, or git diff HEAD to see staged and unstaged together.[1]
Expecting a brand-new file to appear. The default form compares the working tree with the index, and a file that was never added is not in the index. Check git status first. Entries added with git add -N do show up, as an empty existing file in git diff.[1]
Using three dots when two were meant. A..B compares the two tips; A...B starts from their common ancestor.[1]
Passing both --check and --exit-code. The docs say they are not compatible.[1] Pick the one that matches the question: "are there whitespace errors" or "are there any differences."
Applying a diff made with options that make it unappliable. -D omits the preimage of deletes, and the docs say the result is not meant to be applied with patch or git apply.[1] Textconv output and combined-diff output are also for reading only.[1]
Trusting -w to prove a change is safe. It ignores differences even where one line has whitespace and the other has none,[1] so a change that only adds or removes whitespace is invisible with it. Rerun without -w before approving.
Which form to reach for
Before committing, run git diff for what is unstaged, then git diff --cached for what will be recorded. Before opening a pull request, run git diff main...HEAD in the three-dot form to see only your branch's work, and add --stat for a size check. In a script, use --quiet and read the exit code. For rewritten files, add -w or --word-diff to find the real change, then drop the flag to confirm. For a text pair that never lived in Git, use the browser diff checker above. For the commands that act on what a diff shows, such as rebasing and cherry-picking, see the guide to advanced Git commands.
Sources
1. git-diff, Git documentation (accessed December 4, 2026)
Originally published at wowhow.cloud
Top comments (0)