Appearance
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:
| Part | Meaning |
|---|---|
diff --git a/hello.txt b/hello.txt | Git comparing two versions of the same file |
--- a/hello.txt | The "before" version |
+++ b/hello.txt | The "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
| Prefix | Color (if enabled) | Meaning |
|---|---|---|
| (space) | none | Context — unchanged line, shown so you can see where the change happened |
- | red | Removed line |
+ | green | Added 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 diffThis 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 --stagedThis 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 HEADThis 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-baseThe 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..HEADAny 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.jsonThe -- 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
| Mistake | What happens | How to fix |
|---|---|---|
Running git diff after staging | Shows nothing — staging "consumed" the diff. | Use git diff --staged to see staged changes. |
Expecting git diff to show untracked files | It does not. git diff only compares tracked files. | Use git status to see untracked files. |
git diff branch1 branch2 vs branch1..branch2 | Both work. Single-space is equivalent to two-dot. | Pick two-dot or three-dot and be consistent. |
Piping diff output into head or less | The 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 make | Line endings. Windows vs Unix line endings cause false diffs. | Add * text=auto to .gitattributes to normalize line endings. |
Key commands reference
| Command | What it shows |
|---|---|
git diff | Unstaged changes (working tree vs index) |
git diff --staged | Staged changes (index vs last commit) |
git diff HEAD | All uncommitted changes (working tree vs last commit) |
git diff main..branch | Compare branch tip against main |
git diff main...branch | Compare branch against its merge-base with main |
git diff --stat | Summary of changed files (no line-by-line) |
git diff --word-diff | Word-level diffs |
git diff -- <path> | Diff only specific files |
git diff --no-index file1 file2 | Compare two files outside a Git repo |
Related guides
- Making Changes (Add & Commit) — the edit-stage-commit cycle
- Comparing Changes with Git Diff — advanced diff techniques
- Viewing History (git log) — seeing what was committed