Git Diff Explained: Output, Branches, Staged and Two-Dot vs Three-Dot
How to use git diff: what it compares by default, staged changes, two commits, between branches with two or three dots, two files, whitespace options and exit codes.
On this page ⌄
- What does git diff compare? One table for every form
- Staged vs unstaged: why git diff sometimes prints nothing
- Reading the output line by line
- Git diff between two branches: two dots vs three dots
- Git diff between two files
- Summaries: --stat, --name-only, --name-status, --numstat
- Renames, deletes and binary files
- Git diff ignore whitespace
- Word-level diffs: --word-diff
- Moved blocks: --color-moved
- Algorithms: myers, minimal, patience, histogram
- Exit codes for scripts: --exit-code and --quiet
- Apply a git diff
- How to exit git diff, and side by side
- Frequently asked questions
- Tested
- Sources
git diff prints the difference between two states of your project as a unified diff: lines starting with - exist only in the old state, lines starting with + only in the new one. Which two states depends on the arguments. With none, it compares your working tree with the index (what you have not yet staged). The commands below were run in a throwaway repository and the output is pasted as printed.
What does git diff compare? One table for every form
These forms come from the synopsis in the official git-diff manual.
| You want to see | Command | Compares |
|---|---|---|
| Edits you have not staged | git diff | working tree against the index |
| Edits you have staged | git diff --staged (synonym --cached) | index against HEAD |
| Everything since the last commit | git diff HEAD | working tree against HEAD |
| What the last commit changed | git diff HEAD~1 HEAD | two commits |
| Two branch tips | git diff main feature or main..feature | tip against tip |
| What a branch added since it forked | git diff main...feature | merge base against feature |
| One file only | git diff main feature -- greet.js | same, limited to a path |
| Two files outside a repository | git diff --no-index a.txt b.txt | two paths on disk |
Staged vs unstaged: why git diff sometimes prints nothing
After editing greet.js, plain git diff shows the change. After git add greet.js it prints nothing, because the working tree and the index now agree, and git diff --staged shows it instead:
$ git diff # before git add: shows the edit
$ git add greet.js
$ git diff # prints nothing
$ git diff --staged
diff --git a/greet.js b/greet.js
index 9d4c583..d0e2c60 100644
--- a/greet.js
+++ b/greet.js
@@ -1,5 +1,5 @@
function greet(name) {
- return "Hello, " + name;
+ return "Hi, " + name;
}
module.exports = { greet };
If you then edit the file again, git diff shows only the second edit, while git diff HEAD shows both (3 lines in the stat instead of 1 in the run below):
$ git diff --stat
greet.js | 1 +
$ git diff HEAD --stat
greet.js | 3 ++-
A brand-new file that was never added is invisible to git diff. In the test repository git diff --quiet returned exit code 0 with an untracked untracked.txt present and 1 after git add -N untracked.txt ("intent to add"), which also made git diff --stat list untracked.txt | 1 +.
Reading the output line by line
diff --git a/greet.js b/greet.js
index 9d4c583..e2d1c38 100644
--- a/greet.js
+++ b/greet.js
@@ -1,5 +1,5 @@
-function greet(name) {
- return "Hello, " + name;
+function greet(name, punctuation = "!") {
+ return "Hello, " + name + punctuation;
}
module.exports = { greet };
a/andb/are the old and new side, not directories.index 9d4c583..e2d1c38 100644holds abbreviated blob IDs of the old and new content plus the file mode. The IDs depend only on content, so the same file gives the same IDs in any repository.@@ -1,5 +1,5 @@is the hunk header: starting line and line count in the old file, then in the new file. A count of 1 is omitted.- A leading space marks unchanged context. Three lines by default;
-U<n>changes it (the manual says the default comes fromdiff.contextor 3).
Git diff between two branches: two dots vs three dots
Both ranges below use this history (printed by git log --graph --format='%s%d' --all):
* punctuation (feature)
| * main moves on (HEAD -> main)
|/
* readme
* init
main added a licence line to README.md after feature branched; feature changed greet.js.
$ git diff main..feature --stat
README.md | 1 -
greet.js | 4 ++--
2 files changed, 2 insertions(+), 3 deletions(-)
$ git diff main...feature --stat
greet.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
git diff main..feature | git diff main...feature | |
|---|---|---|
| Same as | git diff main feature (confirmed: identical stat) | git diff $(git merge-base main feature) feature (confirmed: identical stat) |
| Answers | How do these two tips differ right now? | What did feature change since it forked? |
README.md | shown as a deleted line, although nobody on feature removed it | not shown |
The manual defines both: A...B "is equivalent to git diff $(git merge-base A B) B", and .. is "synonymous" with the plain two-commit form. It also warns that for git diff these notations are about two endpoints and "do not mean a range" as in git log. For a pull request review use three dots; to see whether two long-lived branches have drifted apart use two.
After the branch is merged, the three-dot diff is empty: in a run that merged feature into main, git diff main...feature --stat printed nothing and exited 0, because the merge base is now the tip of feature. This holds whenever the tip of feature is already reachable from main; if feature gets new commits afterwards, the three-dot diff shows them again.
One file between branches
git diff main feature -- greet.js limits the comparison to that path (anything after -- is a path). To compare a file with a different name or the same file at two refs, name the blobs:
$ git diff main:greet.js feature:greet.js
diff --git a/greet.js b/greet.js
index 9d4c583..e2d1c38 100644
...
Git diff between two files
Inside a repository, git diff <blob> <blob> (above) compares two stored versions. For two ordinary files that git does not track, add --no-index; the manual says you can omit it when at least one path is outside a Git-controlled working tree or when you run the command outside any repository:
$ git diff --no-index a.txt b.txt
diff --git a/a.txt b/b.txt
index 4cb29ea..ea14db2 100644
--- a/a.txt
+++ b/b.txt
@@ -1,3 +1,4 @@
one
-two
+2
three
+four
The manual says this form "implies --exit-code": the run above exited 1, and git diff --no-index a.txt a.txt printed nothing and exited 0. On the Windows machine used here a system-level core.autocrlf=true made git print warning: in the working copy of 'a.txt', LF will be replaced by CRLF the next time Git touches it before each diff; those lines are left out above. They go away if you change that setting.
If you only need to compare two pasted blocks of text and have no files, the browser-based text diff checker shows added and removed lines, words or characters, with options to ignore whitespace and case. It produces no patch file and no side-by-side view.
Summaries: --stat, --name-only, --name-status, --numstat
$ git diff main feature --name-only
README.md
greet.js
$ git diff main feature --name-status
M README.md
M greet.js
$ git diff main feature --shortstat
2 files changed, 2 insertions(+), 3 deletions(-)
--name-only lists the "name of each changed file in the post-image tree"; --name-status adds a status letter. --diff-filter=D keeps only deleted paths: after a commit that removed tmp.txt, git diff HEAD~1 HEAD --diff-filter=D --name-only printed tmp.txt. In CI, git diff --name-only origin/main...HEAD lists the files a branch touched.
Renames, deletes and binary files
Git does not store renames; it detects them when producing a diff. Moving a 10-line report.txt into docs/ unchanged:
$ git diff HEAD~1 HEAD --name-status
R100 report.txt docs/report.txt
$ git diff HEAD~1 HEAD --name-status --no-renames
A docs/report.txt
D report.txt
Moving it back while editing 3 of the 10 lines gave R061 (61% similar). The default similarity threshold is 50% per the manual, so -M90% stopped pairing the files:
$ git diff HEAD~1 HEAD --name-status
R061 docs/report.txt report.txt
$ git diff HEAD~1 HEAD --name-status -M90%
D docs/report.txt
A report.txt
$ git diff HEAD~1 HEAD --stat
docs/report.txt => report.txt | 6 +++---
A deleted file shows deleted file mode, +++ /dev/null and every line as -. A binary file is not diffed line by line:
$ git diff HEAD~1 HEAD -- logo.bin
diff --git a/logo.bin b/logo.bin
new file mode 100644
index 0000000..85025d9
Binary files /dev/null and b/logo.bin differ
$ git diff HEAD~1 HEAD --stat -- logo.bin
logo.bin | Bin 0 -> 5 bytes
$ git diff HEAD~1 HEAD --numstat
1 0 docs/notes.txt
- - logo.bin
0 2 tmp.txt
--numstat prints two - for binary files, as documented. If you need a binary change inside a patch, --binary produces a diff that git apply can apply.
Git diff ignore whitespace
Test change: indentation of one line changed ( return to return) and two blank lines appended to README.md.
$ git diff -w # --ignore-all-space
diff --git a/README.md b/README.md
...
@@ -3,3 +3,5 @@
Licence: MIT
+
+
(greet.js no longer listed)
$ git diff --ignore-blank-lines # README.md no longer listed, greet.js still shown
$ git diff -w --ignore-blank-lines --stat
(no output)
| Option | Ignores | Result on the test |
|---|---|---|
-w / --ignore-all-space | whitespace even where the other line has none | indentation change hidden, blank lines still shown |
-b / --ignore-space-change | changes in the amount of whitespace | same as -w here |
--ignore-blank-lines | changes whose lines are all blank | blank lines hidden, indentation change still shown |
--ignore-cr-at-eol | a carriage return at line end | CRLF to LF change hidden |
The difference between -b and -w: with ab changed to a b, -b still printed the change (-ab / +a b) while -w printed nothing. x y to x y was ignored by -b. A file converted from CRLF to LF showed 2 insertions, 2 deletions in --stat and nothing with --ignore-cr-at-eol.
The ignore options also change the exit status: git diff -w --quiet exited 0 for the ab to a b edit, while git diff --quiet exited 1.
Word-level diffs: --word-diff
$ git diff --word-diff
@@ -1,5 +1,5 @@
function greet(name) {
return [-"Hello,-]{+"Hi there,+} " + name;
}
--word-diff defaults to the plain mode, which wraps removed words in [- -] and added words in {+ +}; the manual notes it does not escape those delimiters if they appear in your text. --word-diff=color uses colour only and --word-diff=porcelain is a line format for scripts. With --word-diff-regex=. the same edit was marked per character: "H{+i ther+}e[-llo-], ".
Moved blocks: --color-moved
Moving a 12-line block below 30 unrelated lines produced a diff with 12 removed and 12 added lines. With --color-moved the moved lines changed colour: in --color=always output the removed block line 12 turned bold magenta (ESC[1;35m) and the added copy bold cyan (ESC[1;36m), instead of red and green. The text and the stat were unchanged, so this is a reading aid only. The manual says the mode is zebra when the option is given without a value, and default is a synonym for it.
Algorithms: myers, minimal, patience, histogram
The default is myers, "the basic greedy diff algorithm". The result differs when the same lines occur more than once. Two functions swapped in swap.c:
$ git diff --diff-algorithm=myers
-void load(void) {
+void parse(void) {
check();
}
-void parse(void) {
+void load(void) {
check();
}
$ git diff --diff-algorithm=histogram
-void load(void) {
- check();
-}
-
void parse(void) {
check();
}
+
+void load(void) {
+ check();
+}
| Algorithm | --stat for the swap |
|---|---|
| myers | 2 insertions, 2 deletions |
| minimal | 2 insertions, 2 deletions |
| patience | 4 insertions, 4 deletions |
| histogram | 4 insertions, 4 deletions |
Myers produced the shorter diff but one that reads as two renamed function headers; patience and histogram produced a longer diff that reads as "load moved below parse". All four describe the same change. Pick by readability: git diff --patience, or set it once with git config diff.algorithm histogram. For how these algorithms work, see how diff tools work.
Exit codes for scripts: --exit-code and --quiet
By default git diff exits 0 whether or not it found differences. --exit-code makes it exit 1 when there are differences; --quiet "disables all output" and implies --exit-code.
| Situation | Exit code in the run |
|---|---|
clean tree, --exit-code or --quiet | 0 |
modified file, --quiet | 1 |
modified file, --exit-code --stat (output still printed) | 1 |
git diff --quiet main main | 0 |
git diff --quiet feature main (different branches) | 1 |
git diff --quiet nosuchref | 128, with fatal: ambiguous argument 'nosuchref' |
--no-index with differences | 1 |
if ! git diff --quiet HEAD -- package-lock.json; then
echo "lockfile changed"
fi
Exit 128 is a git error, not a difference, so scripts that only test for non-zero treat a typo like a change.
Apply a git diff
A diff saved to a file is a patch, and git apply reads it: it "reads the supplied diff output (i.e. "a patch") and applies it to files" without creating a commit (git-apply manual).
$ git diff main...feature > change.patch
$ git apply --check change.patch # exit 0: would apply
$ git apply --stat change.patch
greet.js | 4 ++--
$ git apply change.patch # edits the working tree, no commit
$ git apply --check change.patch # applying twice fails
error: patch failed: greet.js:1
error: greet.js: patch does not apply
--check tests without changing anything. By default a patch that does not fit fails as a whole and leaves the working tree untouched (--reject applies the parts that fit). Use git am for patches made by git format-patch.
How to exit git diff, and side by side
When output is long, git usually shows it in a pager, which is why the prompt may not come back; the git manual names less (or $PAGER if set) for --paginate. If you are in less, q quits it (a key of less, not documented by git). git --no-pager diff does not pipe output into a pager, and the manual says an empty or cat GIT_PAGER stops one launching. git diff itself prints the unified format only; the git-diff manual describes no side-by-side mode. git difftool is "a frontend to git diff" that opens an external viewer such as meld or vimdiff.
Frequently asked questions
What is the use of git diff?
It shows exactly what changed between two states of a repository: working tree and index, index and HEAD, two commits, two branches, or two files. Use it before staging, before committing and when reviewing a branch.
How do I use git diff properly?
Pick the pair of states first: git diff (unstaged), git diff --staged (about to be committed), git diff main...feature (what a branch adds). Add --stat for a summary and -- <path> to limit files.
How do I do a git diff between two branches?
git diff main feature compares the tips. git diff main...feature compares feature with the merge base, so it shows only what feature added.
How do I do a git diff between two files?
Outside a repository or for untracked files, git diff --no-index a.txt b.txt. For two versions of one tracked file, git diff main:path feature:path or git diff main feature -- path.
How do I see only the file names?
git diff --name-only, or --name-status for M, A, D and R letters.
How do I make git diff ignore whitespace?
-w ignores all whitespace, -b ignores changes in its amount, --ignore-blank-lines ignores blank-line changes and --ignore-cr-at-eol ignores CRLF versus LF.
Why does git diff show nothing?
The change is already staged (use --staged), the file is untracked, both sides are the same commit, or a whitespace option hid the change.
How do I apply a git diff?
Save it with git diff > file.patch and run git apply --check file.patch, then git apply file.patch.
Tested
Run on 2026-11-17 with git 2.53.0.windows.1 in Git Bash on Windows 11, in a new repository with a scripted history (two branches, a rename, a delete, a binary file, whitespace-only edits, a merge). Output is pasted as printed, except that commit hashes are left out and the LF to CRLF warnings are omitted. Blob IDs such as 9d4c583 depend only on file content. Rename similarity (R061), exit codes and stat counts come from those runs. Behaviour of --color-moved was checked by reading ANSI escape codes in --color=always output, not on screen. Not tested: Linux or macOS output, external diff tools, git difftool, git am, submodules, and any hosting website's diff view.
Sources
- Git project: git-diff manual, forms of the command,
..,...,--no-index. - Git project: diff options,
--exit-code,--quiet,--word-diff,-M, whitespace options,--diff-algorithm,--color-moved,--numstat. - Git project: git-apply manual,
--check,--stat, atomic failure. - Git project: git difftool manual and the git command page for
--no-pagerandGIT_PAGER.
Keep reading
Related tools
Text Diff
Compare two text blocks line-by-line or word-by-word. Unified and split view. Shows added, removed, and changed segments with full color coding.
Hours Calculator
Add start, end and break times for each day to see daily hours, weekly totals and overtime instantly.
Character Counter
Count characters, words, sentences, lines and UTF-8 bytes with live SEO and social media limit tracking.
Keyboard Test
Press any key to see it light up and find dead, stuck or double-typing keys, with rollover checks for full size, TKL and 75% layouts.
Written by Mian Ali Khalid. More guides in Dev Productivity.