Git Diff Tutorial - Unstaged, --staged, Between Commits and Branches, --stat, --word-diff and --check, with Real Output from git 2.43.0 and the Empty-Diff Surprise

TL;DR

  • git diff compares two things and prints what is different. With no arguments it compares your working tree to the staging area (the index). With --staged it compares the index to the last commit. With HEAD it compares the working tree to the last commit. The git diff reference lists every form.
  • git diff printing nothing does not mean "no changes". Right after git add, plain git diff is empty because the change moved into the index; use git diff --staged (section 3). New untracked files never show up in any git diff (section 8).
  • Two dots versus three dots matter for branches. git diff main..feature compares the two tips; git diff main...feature compares feature against the common ancestor, which shows only what the feature branch added. In my run the first showed 2 files with 1 deletion, the second 2 files with 0 deletions (section 5).
  • The output options I use daily: --stat for a summary, --name-only or --name-status for file lists, --word-diff for prose and config, -w to ignore whitespace, --check before committing. The context default is 3 lines, changed with -U<n>.
  • For scripts, use the exit code. --exit-code returns 1 when there are differences and 0 when there are none.
  • Not your case? If you want to see the commit history instead of a diff, start with the Git and GitHub blueprint course; if you need to hide half-finished changes before you diff, read git stash.

Table of Content

  1. What git diff compares
  2. Set up a small repository
  3. Unstaged and staged changes
  4. Between two commits, and one file only
  5. Between two branches: two dots versus three dots
  6. Summaries: stat, shortstat, numstat, name-only and name-status
  7. Word diff, whitespace and context lines
  8. What broke when I ran this
  9. Exit codes and scripting
  10. Conclusion

1. What git diff compares

Git keeps your work in three places: the last commit (HEAD), the staging area (the index) and the files on disk (the working tree). Every git diff form is a choice of two of those, or of two commits. Once you know which pair a form compares, you can predict whether it will print anything.

git diff compares HEAD, the index and the working tree in pairs

This table is the cheat sheet I keep in my head. The forms come from the official git diff page.

CommandLeft sideRight sideTypical use
git diffIndexWorking treeWhat I changed but have not staged
git diff --staged (same as --cached)Last commitIndexWhat my next commit will contain
git diff HEADLast commitWorking treeEverything I changed since the last commit
git diff <commit> <commit>Commit ACommit BCompare two points in history
git diff A..BCommit ACommit BSame as the line above
git diff A...BMerge base of A and BCommit BWhat branch B added since it left A
git diff --no-index <path> <path>File or directoryFile or directoryTwo paths outside Git

The reference says --staged is a synonym of --cached, and that if HEAD does not exist yet (an unborn branch) and you give no commit, --staged shows all staged changes.

2. Set up a small repository

Everything below ran on git 2.43.0 on 2026-12-05, in an empty directory. If you are new to repositories, What Is Git explains what a commit actually stores. I use a tiny config file and a README so the diffs fit on the page.

 1mkdir demo && cd demo
 2git init -b main
 3cat > app.txt <<'EOT'
 4name: shop
 5version: 1.0
 6port: 8080
 7replicas: 2
 8EOT
 9printf '# Shop\nA small demo service.\n' > README.md
10git add . && git commit -m "initial commit"

3. Unstaged and staged changes

I change the port from 8080 to 9090 and run a bare git diff:

 1$ sed -i 's/port: 8080/port: 9090/' app.txt
 2$ git diff
 3diff --git a/app.txt b/app.txt
 4index a1b09d8..2f5607a 100644
 5--- a/app.txt
 6+++ b/app.txt
 7@@ -1,4 +1,4 @@
 8 name: shop
 9 version: 1.0
10-port: 8080
11+port: 9090
12 replicas: 2

Reading it from the top: the diff --git line names the two files, the index line holds the short blob hashes before and after, --- and +++ are the old and new file, and @@ -1,4 +1,4 @@ says the hunk starts at line 1 and covers 4 lines on each side. Lines starting with - are gone, lines starting with + are new, and lines with a leading space are context.

Now I stage the file and run the same command again:

 1$ git add app.txt
 2$ git diff
 3$ git diff --staged
 4diff --git a/app.txt b/app.txt
 5index a1b09d8..2f5607a 100644
 6--- a/app.txt
 7+++ b/app.txt
 8@@ -1,4 +1,4 @@
 9 name: shop
10 version: 1.0
11-port: 8080
12+port: 9090
13 replicas: 2

The first git diff printed nothing. The second one printed the same hunk as before, because the change moved from "working tree versus index" to "index versus last commit". The index hash 2f5607a is identical in both outputs: it is the same content, only the comparison changed. This is the single most common reason people think git diff is broken, and it is the first item in section 8.

If I edit the file again after staging, I get both views at once: git diff shows only the new edit, git diff --staged shows what is queued, and git diff HEAD shows the sum of both. When I want to park the unstaged part before committing, git stash is the tool.

4. Between two commits, and one file only

I commit the port change, then make a second commit that scales the app and edits the README. The log has three commits:

1$ git log --oneline
2afe75eb scale to 3 replicas, bump version
35e9dc39 move to port 9090
46548fed initial commit

To see everything that changed across the last two commits I compare HEAD~2 with HEAD:

 1$ git diff HEAD~2 HEAD
 2diff --git a/README.md b/README.md
 3index ba91a0c..f0a61cc 100644
 4--- a/README.md
 5+++ b/README.md
 6@@ -1,2 +1,3 @@
 7 # Shop
 8 A small demo service.
 9+extra line
10diff --git a/app.txt b/app.txt
11index a1b09d8..8263157 100644
12--- a/app.txt
13+++ b/app.txt
14@@ -1,4 +1,4 @@
15 name: shop
16-version: 1.0
17-port: 8080
18-replicas: 2
19+version: 1.1
20+port: 9090
21+replicas: 3

Notice that git folded the two commits together: port: 8080 goes straight to 9090, with no trace of the intermediate commit. A diff between two commits is a snapshot comparison, not a history. If you want each commit on its own, use git log -p.

To narrow the output to one file, put -- and a path after the revisions. The -- stops Git from guessing whether the word is a file or a branch name, which saves you from confusing errors when a branch and a file share a name:

1$ git diff HEAD~2 HEAD -- README.md
2diff --git a/README.md b/README.md
3index ba91a0c..f0a61cc 100644
4--- a/README.md
5+++ b/README.md
6@@ -1,2 +1,3 @@
7 # Shop
8 A small demo service.
9+extra line

Revisions can be anything Git resolves: HEAD~3, a tag, a branch, origin/main or a short hash. The syntax is documented in gitrevisions.

5. Between two branches: two dots versus three dots

I create a feature branch with two commits (a log: debug line in app.txt and a new feature.txt), go back to main and add an owner: platform line there. Now both branches have moved since they split, which is the normal state of a team repository.

1$ git diff --stat main..feature
2 app.txt     | 2 +-
3 feature.txt | 1 +
4 2 files changed, 2 insertions(+), 1 deletion(-)
5$ git diff --stat main...feature
6 app.txt     | 1 +
7 feature.txt | 1 +
8 2 files changed, 2 insertions(+)

With two dots git compares the two tips. main has the owner line and feature does not, so the diff reports one deletion that the feature branch never made. With three dots git first finds the merge base, the last commit the branches share, and compares that with feature. The documentation puts it as "git diff A...B is equivalent to git diff $(git merge-base A B) B". I verified the base: git merge-base main feature printed afe75eb, the "scale to 3 replicas" commit.

 1$ git diff main...feature -- app.txt
 2diff --git a/app.txt b/app.txt
 3index 8263157..dd2316e 100644
 4--- a/app.txt
 5+++ b/app.txt
 6@@ -2,3 +2,4 @@ name: shop
 7 version: 1.1
 8 port: 9090
 9 replicas: 3
10+log: debug

My rule: for "what will this pull request change" I use three dots, because that is what a reviewer cares about. For "how far apart are these two branches right now" I use two dots. If the branches are still messy, Git branching explained covers creating, merging and deleting them.

6. Summaries: stat, shortstat, numstat, name-only and name-status

On a large change I never read the full patch first. I read a summary, then open the files that matter. I ran all of these on HEAD~2 versus HEAD:

 1$ git diff --stat HEAD~2 HEAD
 2 README.md | 1 +
 3 app.txt   | 6 +++---
 4 2 files changed, 4 insertions(+), 3 deletions(-)
 5$ git diff --shortstat HEAD~2 HEAD
 6 2 files changed, 4 insertions(+), 3 deletions(-)
 7$ git diff --numstat HEAD~2 HEAD
 81	0	README.md
 93	3	app.txt
10$ git diff --name-only HEAD~2 HEAD
11README.md
12app.txt
13$ git diff --name-status HEAD~2 HEAD
14M	README.md
15M	app.txt
OptionOutputWhen I use it
--statPer-file bar graph and a totals lineFirst look at a pull request
--shortstatTotals line onlyRelease notes, quick size check
--numstatTab-separated added and removed countsParsing in a script
--name-onlyFile namesFeeding a linter or formatter
--name-statusA letter (A, M, D, R) and the nameSeeing what was added or deleted

How much do the options cost

I wanted a number for "does --name-only matter on a big change", so I made a second repository with 2,000 files of 200 lines each, appended one line to every file and timed each form on git 2.43.0 (2026-12-05, warm cache, one run each, so treat the figures as rough):

1$ git diff --shortstat
2 2000 files changed, 2000 insertions(+)
3$ git diff | wc -l
418000
CommandTime for 2,000 changed files
git diff (full patch, 18,000 lines)123 ms
git diff --stat121 ms
git diff --shortstat109 ms
git diff --name-only24 ms

The whole .git directory of this test repository was 320 KB, and the patch itself is cheap to generate; what costs time is reading 18,000 lines. --name-only was about 5 times faster in this run (24 ms against 123 ms, a gap of roughly 100 ms), so I use it in scripts that only need paths.

--diff-filter selects by that letter. On the two branches above, --diff-filter=A --name-only main feature printed feature.txt and --diff-filter=M --name-only main feature printed app.txt. The reference lists the letters: Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R) and a few more, and a lower-case letter excludes that kind instead.

7. Word diff, whitespace and context lines

A line diff is the wrong tool for a one-word edit in a long line, which is what most prose and YAML changes are. I changed two words in note.txt and used --word-diff:

1$ git diff --word-diff
2diff --git a/note.txt b/note.txt
3index 2fe6575..dd8a166 100644
4--- a/note.txt
5+++ b/note.txt
6@@ -1 +1 @@
7The quick [-brown-]{+red+} fox jumps over the [-lazy-]{+sleepy+} dog.

The default mode is plain: removed words are in [- -] and added words in {+ +}. The reference lists the other modes (color, porcelain and none). Adding --word-diff-regex=. splits on characters instead of whitespace and gave me The quick [-b-]r[-own-]{+ed+} fox on the same file, which is more precise and a little harder to read.

Whitespace is the next source of noise. -w (--ignore-all-space) hides it. I indented a line by two spaces: plain git diff showed a changed line, while git diff -w --exit-code printed nothing and exited 0.

git diff --check goes the other direction and warns about whitespace errors before they land in a commit:

1$ printf 'The quick brown fox jumps over the lazy dog.   \n' > note.txt
2$ git diff --check
3note.txt:1: trailing whitespace.
4+The quick brown fox jumps over the lazy dog.   
5$ echo $?
62

The reference says it reports conflict markers and whitespace errors (what counts is controlled by core.whitespace, see git config) and exits non-zero when it finds any. I got exit status 2. It is a cheap check in a pre-commit hook.

For context, git shows 3 lines around each change by default, controlled by diff.context or -U<n>. -U0 strips the context completely, which is handy for scripts that only want the changed lines:

1$ git diff -U0
2diff --git a/app.txt b/app.txt
3index d0bb19e..ac3c79d 100644
4--- a/app.txt
5+++ b/app.txt
6@@ -4 +4 @@ port: 9090
7-replicas: 3
8+replicas: 5

8. What broke when I ran this

These are the surprises, in the order they bit me while I prepared the output above.

1. git diff printed nothing right after git add. I thought the staging command had lost my change. It had not; the change was in the index, so the working tree matched the index and there was nothing to show. git diff --staged printed the full hunk. Fix: remember the pair table in section 1. If git diff is empty and git status shows "Changes to be committed", use --staged.

2. A brand-new file did not appear in git diff. I created new.txt and ran git diff --stat. The summary mentioned only note.txt, while git status --short printed:

1$ git diff --stat
2 note.txt | 2 +-
3 1 file changed, 1 insertion(+), 1 deletion(-)
4$ git status --short
5 M note.txt
6?? new.txt

An untracked file has no index entry to compare against, so git diff ignores it until you run git add. After git add new.txt, it shows up in git diff --staged. The --no-index form can compare it with another path, but it does not make it part of the normal diff.

3. The two-dot diff reported a deletion I never made. On my first branch comparison I read main..feature, saw one deletion in app.txt and went looking for the commit that removed a line. No such commit exists on feature: the line is simply missing from the side that lacks it, because main moved on. Fix: use main...feature when you want the branch's own changes.

4. --check exits with 2, and cannot be combined with --exit-code. The documentation says "Not compatible with --exit-code". If you wrap git diff --check in a script, treat any non-zero status as a failure rather than comparing with 1.

5. --no-index paths look odd. Comparing two files that live outside any repository printed a header with the leading slash stripped:

 1$ git diff --no-index /tmp/a.txt /tmp/b.txt
 2diff --git a/tmp/a.txt b/tmp/b.txt
 3index 7898192..6178079 100644
 4--- a/tmp/a.txt
 5+++ b/tmp/b.txt
 6@@ -1 +1 @@
 7-a
 8+b
 9$ echo $?
101

The exit status was 1 without my passing --exit-code; the reference says this form implies it. That is why git diff --no-index is a fine replacement for diff -u in a script.

9. Exit codes and scripting

--exit-code makes git diff behave like the diff command: 0 means no differences, 1 means there are differences. --quiet implies it and prints nothing. In my first repository, the working tree matched the index after staging, so git diff --exit-code returned 0, while git diff --staged --exit-code returned 1 because the index differed from HEAD:

1$ git diff --exit-code > /dev/null; echo "exit=$?"
2exit=0
3$ git diff --staged --exit-code > /dev/null; echo "exit=$?"
4exit=1

I use these patterns:

1# fail a CI job if generated files are out of date
2git diff --exit-code -- docs/ || { echo "run make docs and commit"; exit 1; }
3
4# refuse to commit with whitespace errors
5git diff --cached --check
6
7# list changed files since main for a linter
8git diff --name-only --diff-filter=ACMR main...HEAD

The --diff-filter=ACMR there skips deleted files, which would make a linter fail on a missing path.

10. Conclusion

git diff is one idea with a few switches: pick two things to compare, then pick how much detail you want. The table in section 1 tells you which two things each form compares; the summaries in section 6 keep large changes readable; --word-diff, -w and --check handle the noise and mistakes; and the exit code makes the command scriptable.

GoalCommand
What have I not staged yetgit diff
What will my commit containgit diff --staged
Everything since the last commitgit diff HEAD
What did this branch addgit diff main...feature
Size of a changegit diff --stat
One word changed in a long linegit diff --word-diff
Catch whitespace errorsgit diff --check

The diff is also how you decide what to fix after a mistake: compare first, then use git commit --amend to fix the last commit, reset soft, mixed and hard to move it, or squash to tidy a branch. This is lesson Day 15 of the DevOps Master Course on YouTube, and the full written companion is the free Git and GitHub blueprint course. Every option is documented on the official git diff page; for staging see git add, and for how attributes change diffs see gitattributes.

Did this save you a debugging session? Stay in the loop -