Namraj Pudasaini
Aug 6, 2026
The git diff command compares different states of your code to show exactly what lines have been added, modified, or deleted. It serves as a safety check before you stage or commit code.
View unstaged changes. Compare your current working directory with your staging area.
git diff
View staged changes. Compare your staged changes, after running git add, with your last commit.
git diff --staged
git diff --cached does the exact same thing.
View all local changes. Compare everything in your working directory, staged or unstaged, against your last commit.
git diff HEAD
Compare specific files. Restrict the output to a single file to reduce terminal noise.
git diff path/to/file.txt
Compare two branches. See what is different between the tips of two separate branches.
git diff branch_one branch_two
Compare two specific commits. Use commit hashes to see exactly what changed between two points in history.
git diff 1234abc 5678def
--stat — Show a high-level summary of modified files and line counts instead of the full raw code change.-w — Ignore all whitespace changes, like extra spaces or tabs, to focus purely on code logic.--name-only — Display only the names of the files that changed.--color-words — Show inline word-level changes instead of comparing entire lines.These combine with each other and with everything above, so you can cut a noisy diff down in one go:
git diff -w --stat main feature
When you run git diff, the terminal uses specific markers to show modifications:
-) is content that was removed or modified from the older version.+) is content that was added, or that represents the updated version.A modified line shows up as both — a red line for the old text, then a green line directly beneath it for the new one. Git does not record edits as edits. It only stores removals and additions, and a change to an existing line is simply both at once.
git diff branch_one branch_two and git diff branch_one..branch_two mean the same thing: compare the two branch tips directly against each other.
Three dots behave differently. git diff branch_one...branch_two compares branch_two against the merge base — the commit where the two branches last shared history. It shows only what branch_two added, ignoring anything that landed on branch_one after they diverged.
git diff main...feature
This is usually what you want when reviewing a feature branch, because it matches what a pull request shows you.
If a file and a branch share a name, git cannot tell which one you meant and stops with fatal: ambiguous argument. Put -- before the path to force everything after it to be read as a filename:
git diff -- path/to/file.txt
Every block of changes starts with a line like this:
@@ -12,7 +12,9 @@
The part after the minus sign describes the old version: starting at line 12, covering 7 lines. The part after the plus sign describes the new version: starting at line 12, covering 9 lines. So two lines were added. Git prints a few unchanged lines of context around each change, which is why those counts are larger than the number of lines you actually edited.
Long diffs open in a pager rather than scrolling past. Press space to page down and q to quit. To print output straight to the terminal instead, disable the pager:
git --no-pager diff