git diff
Beginner Git CLI Safe
Summary: git diff compares two of Git's areas and prints the lines that differ. Plain: working tree vs staging area. --staged: staging area vs last commit. Two commits: history vs history.
Safe — read-only.
What it does
Computes the difference between two snapshots and prints it in the unified diff format: one block per file, hunks marked @@, removed lines -, added lines +, context lines with a leading space.
Why it exists
git status says which files changed; git diff says what changed inside them. Reading it before committing is how mistakes stay out of history, and reading it in reviews is how review works.
When to use it
- After editing, before staging: what did I change?
- After staging, before committing: what will the commit contain? (
--staged) - Between commits, branches or tags: what changed since the release? (
git diff v1.2.0 main) - For one file across time:
git diff HEAD~3 HEAD -- docs/faq.md.
When not to use it
- To see a single commit's change:
git showis shorter. - To find which commit changed a line:
git blameorgit log -S.
Syntax
| Form | Compares |
|---|---|
git diff |
working tree vs staging area (unstaged changes) |
git diff --staged (alias --cached) |
staging area vs HEAD (what the next commit contains) |
git diff HEAD |
working tree vs HEAD (everything since the last commit) |
git diff <a> <b> |
commit a vs commit b (hashes, branches, tags, HEAD~n) |
git diff <a>...<b> |
b vs the common ancestor of a and b (what a merge request shows) |
git diff … -- <path> |
restrict to a file or folder |
git diff --stat |
files and counts instead of lines |
git diff --word-diff |
mark changed words instead of whole lines |
git diff --name-only |
just the file names |
Examples
$ git diffdiff --git a/README.md b/README.md
index 19ed536..6516ba6 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Trailguide
-A tiny command-line program that prints hiking trail information for the **Northwind Trails** guide.
+A small command-line program that prints hiking trail information for the **Northwind Trails** guide.
It exists so that people learning Git have something realistic to practice on: documentation to edit,
data to update, a small program to run, and tests that can pass or fail.
$ git add README.md
$ git diff --staged --stat README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)$ git diff --staged --word-diffA [-tiny-]{+small+} command-line program that prints hiking trail information for the **Northwind Trails** guide.$ git diff HEAD --stat README.md | 2 +-
docs/faq.md | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)A binary file:
$ git diff --stageddiff --git a/docs/images/photo.png b/docs/images/photo.png
new file mode 100644
index 0000000..aa860ab
Binary files /dev/null and b/docs/images/photo.png differExpected result
Text; nothing changes. No output means the two sides are identical. Long diffs open the pager (q quits).
Common mistakes
- Empty
git diffafter staging. The change is staged; use--stagedorHEAD. - Path mistaken for a branch. Add
--before paths. - Whole-file diffs. Line endings or reformatting (lesson 4.5);
--statreveals the size,-wignores whitespace. - Two dots vs three dots.
a..bingit logis a range; ingit diff,a..bequalsa b, whilea...bcompares against the common ancestor.
How to undo or recover
Nothing to undo.
In VS Code
Click a file in Source Control for a side-by-side diff (staged or unstaged list); gutter markers in the editor; Inline View toggle. See git diff in three forms.
In IntelliJ IDEA
Select a file in the Commit window and press Ctrl+D (⌘D); in the Log, select two commits and press Ctrl+D to compare them.
Related commands
git status lists the files diff will show · git show is diff for one commit · git log -p is diff per commit · git add -p stages diff hunks.