Git Diff
git diff shows the exact lines that changed. It is the command you'll use most often to review your own work before committing and to understand others' changes.
Which states are compared
Git has three places a file can be in (working folder, staging area, last commit), so there are several diffs:
| Command | Compares | Answers |
|---|---|---|
git diff |
working folder ↔ staging area | what have I changed but not staged? |
git diff --staged (--cached) |
staging area ↔ last commit | what will my next commit contain? |
git diff HEAD |
working folder ↔ last commit | everything changed since the last commit |
git diff A B |
commit A ↔ commit B | what changed between two versions? |
git diff main...feature |
merge base ↔ feature | what did the feature branch change? |
mkdir recipe && cd recipe && git init -q
printf 'Pancakes\n2 eggs\n1 cup flour\n1 cup milk\n' > pancakes.txt
git add . && git commit -q -m "Add pancakes"
sed -i 's/2 eggs/3 eggs/' pancakes.txt
git add pancakes.txt
echo "1 pinch of salt" >> pancakes.txt
echo "=== git diff (unstaged):"
git diff
echo "=== git diff --staged:"
git diff --staged=== git diff (unstaged):
diff --git a/pancakes.txt b/pancakes.txt
index c9003c8..7c6537c 100644
--- a/pancakes.txt
+++ b/pancakes.txt
@@ -2,3 +2,4 @@ Pancakes
3 eggs
1 cup flour
1 cup milk
+1 pinch of salt
=== git diff --staged:
diff --git a/pancakes.txt b/pancakes.txt
index 993c95e..c9003c8 100644
--- a/pancakes.txt
+++ b/pancakes.txt
@@ -1,4 +1,4 @@
Pancakes
-2 eggs
+3 eggs
1 cup flour
1 cup milk
Reading a diff
diff --git a/pancakes.txt b/pancakes.txt <- which file (a = old, b = new)
index 5b3a1c2..9d4e8f0 100644 <- blob hashes and file mode
--- a/pancakes.txt
+++ b/pancakes.txt
@@ -1,4 +1,4 @@ <- hunk: old lines 1-4, new lines 1-4
Pancakes <- context (unchanged), starts with a space
-2 eggs <- removed line
+3 eggs <- added lineA changed line appears as one removal plus one addition. By default Git shows 3 lines of context around each change; -U1 or -U0 shows fewer.
Summaries instead of full diffs
mkdir app && cd app && git init -q
printf 'a\nb\nc\n' > one.txt && printf 'x\n' > two.txt && echo "old" > old.txt
git add . && git commit -q -m "Start"
printf 'a\nB\nc\nd\n' > one.txt
git rm -q old.txt
echo "new" > new.txt && git add -A
git diff --staged --stat
echo "---"
git diff --staged --name-status
echo "---"
git diff --staged --numstat new.txt | 1 +
old.txt | 1 -
one.txt | 3 ++-
3 files changed, 3 insertions(+), 2 deletions(-)
---
A new.txt
D old.txt
M one.txt
---
1 0 new.txt
0 1 old.txt
2 1 one.txt
--name-status uses the same letters as git status: A added, M modified, D deleted, R renamed.
Word diffs
For prose (documentation, Markdown) a line-based diff is clumsy: changing one word marks the whole line. --word-diff shows the changed words inline:
mkdir docs && cd docs && git init -q
echo "Git is a distributed version control system created in 2005." > intro.md
git add . && git commit -q -m "Intro"
echo "Git is a free, distributed version control system created by Linus Torvalds in 2005." > intro.md
git diff --word-diffdiff --git a/intro.md b/intro.md
index 0caff2d..afe65c5 100644
--- a/intro.md
+++ b/intro.md
@@ -1 +1 @@
Git is a {+free,+} distributed version control system created {+by Linus Torvalds+} in 2005.
[-removed-] and {+added+} mark the changes.
Comparing commits and branches
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "v1"
git tag v1.0
echo "v2" > app.txt && echo "docs" > README.md && git add . && git commit -q -m "v2"
git switch -q -c feature && echo "feature" > feature.txt && git add . && git commit -q -m "Feature"
git switch -q main && echo "hotfix" >> app.txt && git commit -q -am "Hotfix"
echo "== changes since the v1.0 release:"
git diff --stat v1.0 main
echo "== main vs feature, two dots (both sides' changes):"
git diff --name-status main feature
echo "== three dots (only what feature changed):"
git diff --name-status main...feature== changes since the v1.0 release:
README.md | 1 +
app.txt | 3 ++-
2 files changed, 3 insertions(+), 1 deletion(-)
== main vs feature, two dots (both sides' changes):
M app.txt
A feature.txt
== three dots (only what feature changed):
A feature.txt
The two-dot diff also "un-does" the hotfix, because feature doesn't have it. The three-dot form compares against the point where feature branched off, which is what you usually want for reviewing a branch.
Useful options
| Option | Effect |
|---|---|
-- path/ |
limit the diff to a file or folder |
-w / --ignore-all-space |
ignore whitespace changes |
--color-words |
colourised word diff in the terminal |
--check |
warn about trailing whitespace and conflict markers |
-M |
detect renames (on by default in recent Git) |
--diff-algorithm=histogram |
often more readable hunks for moved code |
git difftool |
open the diff in a graphical tool |
Test yourself
You staged some changes and kept editing. Which command shows only the staged part?
- git diff
- git diff --staged
- git diff HEAD~1
- git status -v
In a diff, what does a line starting with - mean?
- A comment
- A line that was removed (present in the old version only)
- A line with an error
- An unchanged line
Which diff shows only the changes made on feature since it branched off main?
- git diff main feature
- git diff main...feature
- git diff feature..main
- git diff --staged feature