Appearance
Comparing Changes with Git Diff
You edit a file. You stage part of it. You commit. Before each step, you need to know what Git actually changed. git diff shows you the exact lines added, removed, or modified.
This guide covers the three comparisons you make every day: unstaged vs staged, staged vs committed, and branch vs branch.
The three comparisons
| Command | Compares | Use case |
|---|---|---|
git diff | Working directory vs staging area | What have I changed but not staged? |
git diff --staged (or --cached) | Staging area vs last commit | What will go into the next commit? |
git diff <branch> | Current branch vs another branch | How does my feature branch differ from main? |
Scenario: editing a file through the stages
Start with a tracked file. One commit exists with this content:
Hello, world!1. Edit the file but do not stage
Add a line:
Hello, world!
This is a new feature.bash
git diffdiff --git a/hello.txt b/hello.txt
index e4f5g6h..a1b2c3d 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
Hello, world!
+This is a new feature.The + shows a new line. The @@ -1 +1,2 @@ tells you the context: the original file had 1 line, the new version has 2 lines.
2. Stage the file
bash
git add hello.txt
git diffNo output. The working directory and staging area are now identical.
bash
git diff --stagedShows the same diff that git diff showed before staging. The change is staged and ready for commit.
3. Edit the file again after staging
Add another line:
Hello, world!
This is a new feature.
One more addition.bash
git diffShows only the new line ("One more addition.") as unstaged.
bash
git diff --stagedStill shows the original diff from step 2 (only "This is a new feature."). Two sets of changes exist: one staged, one not.
Comparing branches
You are on a feature branch and need to see what you changed compared to main:
bash
git diff mainThis shows every line that differs between the tip of main and the tip of your current branch. It does not care about common history. It compares the two snapshots directly.
To see only files that changed in a specific directory:
bash
git diff main -- src/components/To see only file names, not the full diff:
bash
git diff --name-only mainsrc/button.tsx
src/header.tsxLimiting diff output
| Flag | What it does |
|---|---|
--name-only | File names only. |
--stat | Summary: which files changed, how many lines. |
--word-diff | Show changes word-by-word instead of line-by-line. |
--ignore-space-change (or -b) | Ignore whitespace-only changes. |
--ignore-all-space (or -w) | Ignore all whitespace differences. |
-U<n> | Show <n> lines of context instead of the default 3. |
When not to use git diff
- You want to see commit history. Use
git log. - You want to compare two arbitrary commits. Use
git diff <commit1> <commit2>. - You want to review changes before pushing. Use
git diff origin/mainto compare against the remote. - You want an interactive review with staging decisions. Use
git add -p.
Common pitfalls
| Pitfall | What happens |
|---|---|
Running git diff on a new (untracked) file | No output. Git only diffs tracked files. Stage with git add first, or use git diff --staged after staging. |
Forgetting --staged before commit | You review unstaged changes instead of what will actually commit. |
git diff main when main has moved | You compare against the local copy of main, which may be stale. Run git fetch origin first, then git diff origin/main. |
| Binary files | git diff shows "Binary files differ" with no detail. Use git diff --stat to see that the file changed. |
Quick reference
bash
# Unstaged changes
git diff
# Staged changes
git diff --staged
# Both staged and unstaged combined (compare working directory to last commit)
git diff HEAD
# Current branch vs main
git diff main
# Specific file across branches
git diff main -- path/to/file
# List changed files only
git diff --name-only main