Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Inspecting History

git diff command

Reference for git diff: compare the working tree, the index and any two commits, use two-dot and three-dot ranges, limit to paths, and script with exit codes.

git diff shows the difference between two states of your project as a unified diff: lines starting with - were removed, lines starting with + were added. Which two states are compared depends on the arguments. With none, it compares your working tree with the index, so it shows what you changed but have not staged yet.

Learning the four "pairs" is most of the command: working tree against index (git diff), index against HEAD (git diff --staged), working tree against a commit (git diff <commit>), and one commit or tree against another (git diff <a> <b>). Formatting options (stat, word diff, algorithms, whitespace) are listed on git diff options.

Terminal
git diff
git diff --staged
git diff <commit-a> <commit-b> [-- <path>]

Try it

Bash Working tree, index and HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Between commits, ranges, paths and exit codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --cached [<commit>] [--] [<path>...]
git diff [<options>] <commit> <commit> [--] [<path>...]
git diff [<options>] <commit>..<commit> [--] [<path>...]
git diff [<options>] <commit>...<commit> [--] [<path>...]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>
Form Compares
git diff Working tree against the index (unstaged changes).
git diff --staged, --cached Index against HEAD, or against <commit> when given (what the next commit would contain).
git diff <commit> Working tree against that commit.
git diff <a> <b>, <a>..<b> Commit a against commit b. The two forms are identical.
git diff <a>...<b> The merge base of a and b against b: the changes made on b since the branches split.
git diff <blob> <blob> Two blobs, for example HEAD:a.txt HEAD~3:a.txt.
--no-index <path> <path> Two files or directories on disk; works outside repositories and ignores Git state.
Option Description
--quiet No output; implies --exit-code.
--exit-code Exit with 1 when there are differences, 0 when not.
--name-only, --name-status, --stat, --shortstat, --numstat Summaries instead of a patch.
-- <path>... Limit the comparison to paths.
--check Report whitespace errors and conflict markers (non-zero exit when found).
-R Reverse the direction of the diff.
--diff-filter=<set> Keep only some change types (A, C, D, M, R, T, U, X, B).

Usage notes

  • Untracked files are not shown by git diff. Stage them with git add -N <file> (intent to add) or look at git status.
  • Two dots versus three dots do not mean what they mean in git log. For diff, A..B is just "A to B", while A...B starts at the merge base. Use three dots to review a pull request's own changes.
  • A path that looks like a revision needs --: git diff main -- main.
  • Exit codes for scripts: plain git diff always exits 0 on success; add --exit-code or --quiet to get 1 for "there are differences".
  • Line endings and whitespace can produce noise; see -w, --ignore-space-at-eol and core.whitespace.
  • Renames are detected by similarity and shown as rename from/to; tune with -M<n>, or switch off with --no-renames. Copy detection is opt-in (-C).
  • Merge conflicts: during a conflicted merge git diff prints a combined diff (--cc) of the conflicted files. --ours, --theirs and --base choose a stage to compare with.
  • Large generated or binary files show as Binary files differ; use --stat, or .gitattributes diff drivers for custom handling.