Skip to content

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 ​

CommandComparesUse case
git diffWorking directory vs staging areaWhat have I changed but not staged?
git diff --staged (or --cached)Staging area vs last commitWhat will go into the next commit?
git diff <branch>Current branch vs another branchHow 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 diff
diff --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 diff

No output. The working directory and staging area are now identical.

bash
git diff --staged

Shows 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 diff

Shows only the new line ("One more addition.") as unstaged.

bash
git diff --staged

Still 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 main

This 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 main
src/button.tsx
src/header.tsx

Limiting diff output ​

FlagWhat it does
--name-onlyFile names only.
--statSummary: which files changed, how many lines.
--word-diffShow 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/main to compare against the remote.
  • You want an interactive review with staging decisions. Use git add -p.

Common pitfalls ​

PitfallWhat happens
Running git diff on a new (untracked) fileNo output. Git only diffs tracked files. Stage with git add first, or use git diff --staged after staging.
Forgetting --staged before commitYou review unstaged changes instead of what will actually commit.
git diff main when main has movedYou compare against the local copy of main, which may be stale. Run git fetch origin first, then git diff origin/main.
Binary filesgit 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

Git Master — Git Reference for Every Skill Level