Viewing Changes With git diff: Unstaged, Staged, Commits, Branches and Word Diffs
How to read a diff and get the one you need: unstaged, staged and all changes, commits, branches with two dots or three, single paths, summaries, word diffs, whitespace and context options, diff algorithms, patches, files outside a repository, and UTF-16 and binary files.
kvi5hnuCode Sprout team member10 min readOn this page
git diff answers "what changed?" between any two versions: your working tree, the staging area, any commit, any branch. This article starts with how to read its output, then covers the comparisons and options you'll use most.
Reading a diff
Git prints differences in the unified diff format, the same format diff -u and code review tools use:

- A diff has one section per file, starting with
diff --git a/<path> b/<path>. Thea/side is the old version and theb/side the new one. - The hunk header
@@ -1,3 +1,4 @@says the hunk covers 3 lines starting at line 1 in the old file, and 4 lines starting at line 1 in the new one. Git often adds the nearest function or heading after the second@@, to show where you are in the file. - Each line inside starts with a space (unchanged context),
-(removed) or+(added). There's no "changed" marker: a changed line appears as the old line removed and the new line added. - By default, a hunk shows 3 lines of context on each side, and changes whose context would overlap, up to 6 unchanged lines apart, merge into one hunk.
In a terminal, Git colours removed lines red and added lines green, and shows long output in a pager: press q to quit, space to page down, and / to search.
What you're comparing
Article 4 introduced the three everyday comparisons:
| Command | Compares | Shows |
|---|---|---|
git diff | staging area → working tree | changes you haven't staged |
git diff --staged (--cached) | HEAD → staging area | what the next commit will contain |
git diff HEAD | HEAD → working tree | all changes since the last commit |
git status -v shows the staged diff below the status, and -vv also shows the unstaged one, labelled separately.
To compare commits, give one or two of them:
| Command | Compares |
|---|---|
git diff abc123 | commit abc123 → working tree |
git diff abc123 def456 (or abc123..def456) | commit abc123 → commit def456 |
git diff HEAD~1 HEAD | the last commit's parent → the last commit: what the last commit changed |
git diff HEAD~3 | three commits ago → working tree |
git diff --staged abc123 | commit abc123 → staging area |
HEAD~1 means one commit before HEAD, and HEAD~3 three before. Article 29 covers all the ways to name commits. For "what did this one commit change?", git show <commit> (article 10) is usually simpler than git diff.
git init -q trail-guide
cd trail-guide
mkdir trails
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails/ridge.txt
git add . && git commit -q -m "Start project"
echo "Lake path: 5 km" > trails/lake.txt
git add . && git commit -q -m "Add lake path"
echo "Notes and maps for local hikes." >> README.md
sed -i 's/8 km/8.5 km/' trails/ridge.txt
git commit -q -am "Update README and ridge distance"
echo "=== what the last commit changed (HEAD~1 -> HEAD)"
git diff --stat HEAD~1 HEAD
echo "=== everything since the first commit"
git diff --stat HEAD~2 HEAD
echo "=== the same comparison, only for the trails/ folder"
git diff HEAD~2 HEAD -- trails/
=== what the last commit changed (HEAD~1 -> HEAD)
README.md | 1 +
trails/ridge.txt | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
=== everything since the first commit
README.md | 1 +
trails/lake.txt | 1 +
trails/ridge.txt | 2 +-
3 files changed, 3 insertions(+), 1 deletion(-)
=== the same comparison, only for the trails/ folder
diff --git a/trails/lake.txt b/trails/lake.txt
new file mode 100644
index 0000000..a5822b6
--- /dev/null
+++ b/trails/lake.txt
@@ -0,0 +1 @@
+Lake path: 5 km
diff --git a/trails/ridge.txt b/trails/ridge.txt
index e4e52a0..e332a7c 100644
--- a/trails/ridge.txt
+++ b/trails/ridge.txt
@@ -1 +1 @@
-Ridge loop: 8 km
+Ridge loop: 8.5 km
Paths after the commits restrict the diff to those files or folders. The -- separates commits from paths. It's optional when the meaning is clear, but needed when a file has the same name as a branch.
Comparing branches: two dots or three
Branches are names for commits, so they compare like commits. There are two different questions, though, and they need different syntax:
git diff main feature, ormain..feature, compares the two branch tips as they are now. Ifmainhas moved on sincefeaturewas created, its new changes appear too, reversed, as iffeaturehad removed them.git diff main...feature, three dots, compares the point wherefeaturebranched off (the merge base) with the tip offeature. It shows only what was done onfeature, which is what a pull request shows.
git init -q trail-guide
cd trail-guide
echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
git switch -q -c feature
echo "Lake path: 5 km" >> trails.txt
git commit -q -am "Add lake path"
git switch -q main
echo "Keep to marked paths." > rules.txt
git add rules.txt && git commit -q -m "Add trail rules"
echo "=== two dots (or none): the tips as they are now"
git diff --stat main feature
echo "=== three dots: only the changes made on feature"
git diff --stat main...feature
=== two dots (or none): the tips as they are now
rules.txt | 1 -
trails.txt | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
=== three dots: only the changes made on feature
trails.txt | 1 +
1 file changed, 1 insertion(+)
With two dots, rules.txt appears as a deletion: it exists on main and not on feature, but nobody deleted it on feature. The branch simply started before it existed. Three dots answers the review question correctly: feature added one line to trails.txt. (git log also accepts .. and ..., with related but different meanings, article 29.)
Summaries instead of full diffs
| Option | Output |
|---|---|
--stat | each changed file with a bar of + and -, and totals |
--shortstat | just the totals line |
--numstat | added and removed line counts per file, tab-separated, for scripts |
--name-only | just the file names |
--name-status | file names with a status letter: A added, M modified, D deleted, R renamed, C copied |
--dirstat | how the changes are spread across folders, in percent |
git init -q trail-guide
cd trail-guide
printf 'Ridge loop\nDistance: 8 km\nClimb: 240 m\nSurface: gravel\n' > ridge.txt
echo "draft" > notes.txt
echo "# Trail guide" > README.md
git add . && git commit -q -m "Start project"
git mv ridge.txt ridge-loop.txt
git rm -q notes.txt
echo "Lake path: 5 km" > lake.txt
echo "Notes and maps." >> README.md
git add -A
echo "=== --name-status"
git diff --staged --name-status
echo "=== --numstat"
git diff --staged --numstat
echo "=== --shortstat"
git diff --staged --shortstat
=== --name-status
M README.md
A lake.txt
D notes.txt
R100 ridge.txt ridge-loop.txt
=== --numstat
1 0 README.md
1 0 lake.txt
0 1 notes.txt
0 0 ridge.txt => ridge-loop.txt
=== --shortstat
4 files changed, 2 insertions(+), 1 deletion(-)
R100 means a rename with 100% similarity. The rename shows as 0 0 in --numstat, because no lines changed.
Word diffs for prose and long lines
For text such as documentation, where one word changes in a long line, a line-based diff hides the change. --word-diff marks changes within the line:
git init -q trail-guide
cd trail-guide
echo "The Ridge loop is 8 km long and mostly flat." > ridge.txt
git add . && git commit -q -m "Describe ridge loop"
echo "The Ridge loop is 8.5 km long and gently rolling." > ridge.txt
echo "=== line diff"
git diff | tail -2
echo "=== word diff"
git diff --word-diff | tail -1
=== line diff
-The Ridge loop is 8 km long and mostly flat.
+The Ridge loop is 8.5 km long and gently rolling.
=== word diff
The Ridge loop is [-8-]{+8.5+} km long and [-mostly flat.-]{+gently rolling.+}
[-...-] marks removed words and {+...+} added ones. --word-diff=color, or the shortcut --color-words, uses only colour, with no markers, which is easiest to read in a terminal. --word-diff=porcelain produces a format for scripts. --word-diff-regex=<regex> changes what counts as a word, for example --word-diff-regex=. to compare character by character.
Whitespace, context and algorithms
Whitespace. Reindenting code changes every line, and hides the real change. These options compare without it:
| Option | Ignores |
|---|---|
-w, --ignore-all-space | all whitespace differences |
-b, --ignore-space-change | changes in the amount of whitespace |
--ignore-space-at-eol | whitespace at line ends |
--ignore-blank-lines | added or removed blank lines |
--ignore-cr-at-eol | Windows \r at line ends |
git diff --check does the opposite: it reports whitespace errors your change introduces, such as trailing spaces, and leftover conflict markers, and exits with a non-zero status if it finds any. It's a common pre-commit check.
Context. -U<n> shows n lines of context instead of 3: -U0 gives only the changed lines, and -U10 more surroundings. -W (--function-context) shows the whole function around each change.
Algorithms. Git can compute diffs with several algorithms. The default, myers, is fast. histogram and patience often produce more readable diffs for code, when lines such as } repeat, by matching unique lines first. minimal tries harder to produce the smallest diff. Choose one with --diff-algorithm=histogram, or set git config --global diff.algorithm histogram, which many developers do.
Moved code. --color-moved colours blocks of lines that moved, rather than showing them as unrelated deletions and additions. Set diff.colorMoved=default to make it permanent.
Diffs as patches
A diff is also a patch: a file that can be applied to another copy of the code. git diff > fix.patch saves one, and git apply fix.patch applies it in another working tree. Patches made by Git use a/ and b/ prefixes, which git apply understands, and so does the classic patch tool with patch -p1 < fix.patch. git diff --no-prefix drops the prefixes for tools that expect bare paths, applied with patch -p0. For binary files, add --binary. Article 45 covers patches, including git format-patch, which keeps the commit message and author too.
Comparing files outside a repository
git diff --no-index a b compares any two files or folders, even outside a repository, with all of Git's options: word diffs, colour, --stat. It's a handy replacement for the diff command:
printf 'Ridge loop: 8 km\nLake path: 5 km\n' > old-list.txt
printf 'Ridge loop: 8 km\nLake path: 5.5 km\nForest walk: 3 km\n' > new-list.txt
git diff --no-index old-list.txt new-list.txt
echo "exit status: $?"
diff --git a/old-list.txt b/new-list.txt
index 8a303ba..f55397d 100644
--- a/old-list.txt
+++ b/new-list.txt
@@ -1,2 +1,3 @@
Ridge loop: 8 km
-Lake path: 5 km
+Lake path: 5.5 km
+Forest walk: 3 km
exit status: 1
The exit status is 1 because the files differ. git diff --exit-code, which --no-index implies, and --quiet, which also suppresses the output, make git diff usable in scripts: status 0 means no differences.
Binary files and other encodings
For files Git considers binary, such as images, it prints only "Binary files ... differ". Some files are text that Git can't read directly, such as UTF-16 files like the .strings localization files used by iOS and macOS, or binary property lists. A textconv driver tells Git how to turn such files into text for diffing. It's two pieces of configuration: a driver in your Git config, and a .gitattributes line that assigns it to files:
git init -q app
cd app
printf 'Welcome\n' | iconv -f utf-8 -t utf-16 > Localizable.strings
git add . && git commit -q -m "Add strings"
printf 'Welcome back\n' | iconv -f utf-8 -t utf-16 > Localizable.strings
echo "=== without a driver"
git diff
git config diff.utf16.textconv "iconv -f utf-16 -t utf-8"
echo '*.strings diff=utf16' > .gitattributes
echo "=== with the utf16 driver"
git diff Localizable.strings
=== without a driver
diff --git a/Localizable.strings b/Localizable.strings
index 37c0bf1..488f8fe 100644
Binary files a/Localizable.strings and b/Localizable.strings differ
=== with the utf16 driver
diff --git a/Localizable.strings b/Localizable.strings
index 37c0bf1..488f8fe 100644
--- a/Localizable.strings
+++ b/Localizable.strings
@@ -1 +1 @@
-Welcome
+Welcome back
The same pattern works for other formats. For binary property lists on macOS, use textconv = plutil -convert xml1 -o -. For Word documents or PDFs, use a converter that prints their text. The conversion only affects how diffs are displayed: the stored file is unchanged. Article 35 covers .gitattributes in depth.
Graphical diffs
git difftool shows the same comparisons in a graphical tool such as Meld, Beyond Compare, KDiff3, VS Code or an IntelliJ IDE. It takes the same arguments as git diff: git difftool --tool=meld --dir-diff opens all working-tree changes at once, and git difftool --dir-diff main...feature shows a branch's changes. Article 37 explains how to configure diff tools.
What's current
histogramis widely recommended asdiff.algorithmfor code. Git 2.53 added--diff-algorithmtogit blametoo, so blame and diff can use the same algorithm.--color-movedand--color-moved-wsmake refactorings readable in the terminal.- Git 2.53 added a whitespace error class,
incomplete-line, forgit diff --checkandgit apply, to flag a missing newline at the end of a file when you choose to treat that as an error.
Common questions
Why does git diff show nothing after I staged my changes?
git diff compares the working tree with the staging area. Once changes are staged, they match. Use git diff --staged for staged changes, or git diff HEAD for everything since the last commit.
What's the difference between git diff A..B and git diff A...B?
A..B is the same as A B: the two commits as they are. A...B compares the merge base of A and B with B, so it shows only the changes made on B since it diverged from A. For reviewing a branch, use three dots.
How do I see changes for just one file?
Put the path after the other arguments: git diff -- trails.txt, git diff HEAD~3 HEAD -- trails/. The same works for git log and git show.
Why does my diff show every line of a file as changed?
Usually line endings (one side has Windows \r\n, the other \n) or reindentation. Try git diff --ignore-cr-at-eol or -w to confirm, then fix the cause: article 35 covers line endings.
How do I count how many lines a branch added?
git diff --shortstat main...feature gives the totals, and --numstat gives per-file counts.
Summary
- A diff shows hunks of context lines,
-removed lines and+added lines, with@@headers giving the line ranges. git diff,git diff --stagedandgit diff HEADcompare the working tree, staging area and last commit. Give one or two commits to compare history.main...featureshows only the branch's own changes, andmain featurecompares the two tips as they are.--stat,--name-statusand--numstatsummarize.--word-diffshows changes within lines.-wand friends ignore whitespace.diff.algorithm=histogramand--color-movedmake code diffs easier to read.git diff --no-indexcompares any files, and textconv drivers make special formats diffable.
Practice questions
You're reviewing a teammate's branch feature/maps, which was created from main two weeks ago. main has had many commits since. Which command shows only the teammate's changes?
Answer
git diff main...feature/maps. Three dots compare the merge base, where the branch started, with the branch tip, so changes made on main since then are left out. git diff main feature/maps would mix in all of main's newer changes, shown in reverse.
What does this hunk header mean: @@ -10,4 +10,6 @@?
Answer
The hunk covers 4 lines of the old file starting at line 10, and 6 lines of the new file starting at line 10. Two more lines exist after the change than before: for example, two lines added, or three added and one removed.
A reformatting tool reindented a whole file and you also fixed one bug in it. How can you see just the bug fix in the diff?
Answer
git diff -w (or --ignore-all-space), which ignores whitespace differences, so the reindented lines disappear and only the real change remains. -b ignores changes in the amount of whitespace but not added or removed whitespace. Better still for the history: commit the reformatting and the fix separately.
Previous: Committing: Good Messages, Amending, Empty Commits, Authors and Dates
Next: Ignoring Files: .gitignore Patterns, Global and Local Ignores, and Untracking Files

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.