Skip to content

Git Diff: Seeing What Changed ​

You changed a file. What exactly changed? git diff shows you the difference between two versions of a file, down to the individual line. This guide covers reading diff output, comparing the three states of a file, and the common scenarios where git diff answers a specific question.

What a diff looks like ​

diff
diff --git a/hello.txt b/hello.txt
index e69de29..2e65efe 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello, world!

Line by line:

PartMeaning
diff --git a/hello.txt b/hello.txtGit comparing two versions of the same file
--- a/hello.txtThe "before" version
+++ b/hello.txtThe "after" version
@@ -0,0 +1 @@The hunk header — which lines changed
+Hello, world!A line that was added (prefixed with +)

Hunk headers explained ​

@@ -0,0 +1 @@ reads as:

  • -0,0 — the old file had no lines at this position
  • +1 — the new file has 1 line starting at line 1

After a few edits, a hunk header might say @@ -3,7 +5,10 @@, meaning "in the old file, lines 3-9 changed; in the new file, those became lines 5-14."

The three line prefixes ​

PrefixColor (if enabled)Meaning
(space)noneContext — unchanged line, shown so you can see where the change happened
-redRemoved line
+greenAdded line

The three comparisons you will make ​

Git can compare any pair of these three states:

Working directory  ←→  Staging area (index)  ←→  Last commit (HEAD)

1. What is unstaged? (working tree vs staging area) ​

bash
git diff

This shows changes you made since the last git add. Nothing has been staged yet. Example output:

diff
diff --git a/hello.txt b/hello.txt
index 2e65efe..a3b5c7d 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
 Hello, world!
+This line is not staged yet.

2. What is staged? (staging area vs last commit) ​

bash
git diff --staged

This shows changes you already staged with git add but have not committed. Use this before git commit to double-check what you are about to save.

diff
diff --git a/hello.txt b/hello.txt
index e69de29..2e65efe 100644
--- a/hello.txt
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello, world!

3. Full picture (working tree vs last commit) ​

bash
git diff HEAD

This shows everything — staged and unstaged — combined into one diff against the last commit. Useful for a quick "what have I done since my last commit?"

Comparing branches ​

bash
git diff main..feature-x
git diff main...feature-x   # compare against the merge-base

The two-dot form (..) compares the tip of feature-x against the tip of main. The three-dot form (...) finds the common ancestor of both branches and compares feature-x against that. Three-dot is usually what you want when reviewing a pull request — it shows only the changes the feature branch actually introduced, not everything that happened on main since it diverged.

Comparing arbitrary commits ​

bash
git diff a1b2c3d..e5f6a7b
git diff HEAD~3..HEAD

Any commit reference works: SHAs, branch names, tags, relative references.

Limiting diff to specific files ​

bash
git diff -- src/ src/index.ts
git diff HEAD~1 -- package.json

The -- separates tree-ish arguments from path arguments. Everything after -- is a file path, not a commit.

Making diff output easier to read ​

bash
# Show word-level diffs instead of line-level
git diff --word-diff

# Show a summary of changed files (no line-by-line)
git diff --stat

# Ignore whitespace changes
git diff --ignore-space-change
git diff --ignore-all-space

--stat output:

text
 hello.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

What's happening here ​

When you run git diff, Git does not store diffs anywhere. It reads two snapshots (from the working tree, the index, or a commit's tree object), runs a diff algorithm, and prints the result. No state changes, no side effects. Running git diff a hundred times produces the same output if nothing else changed.

The default diff algorithm is Myers. It finds the shortest edit sequence that turns one file into the other. If you see strange alignment in diffs of heavily rearranged code, try git diff --patience for cleaner output.

Common mistakes ​

MistakeWhat happensHow to fix
Running git diff after stagingShows nothing — staging "consumed" the diff.Use git diff --staged to see staged changes.
Expecting git diff to show untracked filesIt does not. git diff only compares tracked files.Use git status to see untracked files.
git diff branch1 branch2 vs branch1..branch2Both work. Single-space is equivalent to two-dot.Pick two-dot or three-dot and be consistent.
Piping diff output into head or lessThe output is already paged by Git's default pager.Set git config --global core.pager cat to disable paging.
Diff shows changes you did not makeLine endings. Windows vs Unix line endings cause false diffs.Add * text=auto to .gitattributes to normalize line endings.

Key commands reference ​

CommandWhat it shows
git diffUnstaged changes (working tree vs index)
git diff --stagedStaged changes (index vs last commit)
git diff HEADAll uncommitted changes (working tree vs last commit)
git diff main..branchCompare branch tip against main
git diff main...branchCompare branch against its merge-base with main
git diff --statSummary of changed files (no line-by-line)
git diff --word-diffWord-level diffs
git diff -- <path>Diff only specific files
git diff --no-index file1 file2Compare two files outside a Git repo

Git Master — Git Reference for Every Skill Level