inspection
git diff
Shows line-level differences between commits, the index, and the working tree. Without arguments, compares unstaged changes to the index.
git diff [<options>] [<commit>] [--] [<path>...] Common flags
| Flag | Purpose |
|---|---|
| --staged / --cached | Diff the index against HEAD (what would be committed) |
| --stat | Summary of changed files and line counts |
| --name-only | Just list changed file paths |
| -w / --ignore-all-space | Ignore whitespace differences |
| --word-diff | Highlight word-level rather than line-level changes |
Examples
git diff Unstaged changes in the working tree
git diff --staged What will go into the next commit
git diff main..feature -- src/ Compare two branches for a subdirectory
Gotcha
In `git diff`, A..B compares the two commits directly (same as `git diff A B`), while A...B diffs from their merge-base to B — different semantics from `git log`, not just reversed.
Related commands
git log
Displays commit history in reverse chronological order with filters for author, date, path, or content. The primary tool for exploring project history.
git status
Shows the state of the working tree and index: staged, unstaged, and untracked files, plus branch tracking info. The first command to run when you're unsure what changed.
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.