By admin , 29 April 2026

Synopsis

git diff [<options>] [<commit>] [--] [<path>...]

Description

The git diff command shows differences between two sets of file content. With no arguments, it compares the working tree against the staging area, showing what is unstaged. With --cached (or --staged), it compares the staging area against the latest commit. With one or two commit arguments, it compares those commits.

Diffs are central to code review, debugging, and understanding history. Git produces unified diffs by default but can also output word-level diffs, color-moved highlighting, and statistics. For binary files, only a "differ" line is shown unless a binary diff driver is configured.

In day-to-day use, git diff integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git diff --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git diff interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git diff's side effects helps avoid all three.

Common Options

OptionDescription
--cached / --stagedCompare the index to HEAD instead of working tree to index.
--statShow a summary of insertions/deletions per file.
--name-onlyList only the file names that differ.
--word-diffHighlight changes at the word level.
-w, --ignore-all-spaceIgnore whitespace differences.
--color-movedHighlight blocks of code that were moved.
-U<n>Show n lines of context (default 3).

Examples

git diff
# Unstaged changes in the working tree

git diff --cached
# Changes staged for the next commit

git diff main..feature
# All differences between two branches

git diff HEAD~3 HEAD -- src/
# Last three commits, restricted to src/

Common Mistakes

Forgetting --cached when reviewing what is about to be committed is a classic confusion. Another is using two-dot vs three-dot range syntax incorrectly: A..B is "differences from A to B," while A...B is "differences from the merge base to B." Massive whitespace-only diffs after editor changes can be tamed with -w.

Related Commands

git status, git log -p, git show, git range-diff