git diff
Show changes between your files, the staging area and commits
The 'git diff' command shows the exact lines that changed. Without arguments it compares your working files with the staging area — the changes you have not staged yet. With --staged it compares the staging area with the last commit — what your next commit will contain. Give it commits or branches to compare any two versions of the project.
Synopsis
git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --staged [<commit>] [--] [<path>...]
git diff [<options>] <commit> <commit> [--] [<path>...]Options
Choosing the Right Option
What git diff compares
Pick the two sides you want to compare
| Command | Left side | Right side |
|---|---|---|
git diff | Staging area | Working files |
git diff --staged | Last commit | Staging area |
git diff HEAD | Last commit | Working files |
git diff A B | Commit A | Commit B |
Examples
Unstaged changes
git diffWhat changed in your files but is not staged yet.
Staged changes
git diff --stagedWhat the next commit will contain.
Two commits
git diff HEAD~1 HEADWhat the last commit changed.
Two branches
git diff main feature/loginEverything that differs between two branches.
Workflow Examples
git diffRead what you changed
git add app.jsStage it
git diff --stagedRead what will be committed
git commit -m "Add to cart"Commit
git diff main...feature/login --statFiles the branch changed since it split from main
git diff main...feature/loginThe full changes
Troubleshooting
git diff shows nothing but files changed
Cause: The changes are already staged
Use git diff --staged (or git diff HEAD for both)
A new file is missing from the diff
Cause: Untracked files are not compared
Stage it with git add, then use git diff --staged
The whole file shows as changed
Cause: Line endings or whitespace changed
Try git diff -w to ignore whitespace and check core.autocrlf
Tips & Pitfalls
Best Practices
- ✓Review git diff --staged before every commit.
- ✓Use --stat first on big changes, then read the details.
- ✓Compare branches with three dots (main...branch) to see only what the branch added.
Common Mistakes
- ✗Expecting git diff to show staged changes — it does not; use git diff --staged.
- ✗Expecting untracked files in git diff: new files appear only once they are added.
- ✗Mixing up the order of two commits: git diff A B shows how to go from A to B.
FAQ
git diff --staged (or its synonym git diff --cached).
git diff HEAD compares the last commit with your working files.
git show <commit>, or git diff <commit>~1 <commit>.