Home·Terminal commands

git diff

git

Usage

git diff [--staged] [<commit>] [-- <path>]

Shows only the changes you have not staged yet, which is why it looks empty right after git add — use --staged then.

Common flags

FlagMeaning
--stagedcompare the staged snapshot with HEAD (same as --cached)
--statsummary of files and line counts instead of the patch
-wignore whitespace changes
--name-onlylist changed paths only
<a>...<b>changes on b since it forked from a
-- <path>restrict to a path; the -- keeps it from being read as a branch

Examples

git diff

shows only what is NOT staged yet

git diff --staged

shows what git commit would record

git diff main...feature

the work feature added since it left main

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git diff do?

Shows only the changes you have not staged yet, which is why it looks empty right after git add — use --staged then.

Q. How do I type it?

git diff [--staged] [<commit>] [-- <path>] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-diff