Git Diff
Last updated
git diff shows the exact line-by-line changes between two states of your repository. With no arguments it shows what you've changed but not yet staged. Add --staged to see what's staged, name two commits or branches to compare them, or name a file to narrow it down.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git diff | Unstaged changes vs the index |
git diff --staged | Staged changes vs the last commit |
git diff HEAD | All changes since the last commit |
git diff main feature | Difference between two branches |
git diff <hash1> <hash2> | Difference between two commits |
git diff -- file.txt | Changes to a single file |
Common cases
| Goal | Command |
|---|---|
| See what you're about to commit | git diff --staged |
| See uncommitted work | git diff |
| Compare your branch to main | git diff main |
| Just the file names that changed | git diff --name-only |
Git diff FAQ
What does git diff show by default?
With no arguments,
git diff shows changes in your working tree that are not yet staged - what you've edited since the last git add. To see what you've already staged (and will commit next), use git diff --staged; to see everything since the last commit, use git diff HEAD.What's the difference between git diff and git diff --staged?
git diff compares your working tree to the staging area (unstaged changes). git diff --staged (also --cached) compares the staging area to the last commit (staged changes). Together they show the two halves of your uncommitted work.How do I diff two branches or commits?
Name them:
git diff main feature shows what differs between the two branches, and git diff <hash1> <hash2> compares two commits. Add -- <file> at the end to limit the comparison to one file.How do I see only which files changed, not the full diff?
Use
git diff --name-only for just the filenames, or git diff --stat for a summary showing each file and how many lines changed. These are handy for a quick overview before reading the full line-by-line diff.Can I practice this online?
Yes. Open the terminal playground to run
git diff in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers inspecting changes step by step.