Git Diff: Comparing Changes

Updated · 12 min read
easyGit 2.53gitgit-diffstagedword-diffinterview

A teacher checking a corrected essay doesn't read the whole essay again. They look for the red marks: the words crossed out and the words written in. Everything else is the same as before.

git diff gives you those red marks for your project. Instead of rereading every file, you see only the lines that were removed and the lines that were added. You will use it before every commit, to check that you are about to save exactly what you meant to, and nothing else.

In this lesson, you will learn the four comparisons you need every day, and a few options that make a diff easier to read.

The Starting Point

Navika's project has two commits:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
printf 'Term 1 fee: $12,000\nTerm 2 fee: $12,000\nTerm 3 fee: $12,000\n' > fees.txt
git add .
git commit --quiet -m "Add README and the term fees"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git log --oneline
Output
f585e6f (HEAD -> main) Add the late fee
4f1489e Add README and the term fees

What Have I Changed? git diff

Navika corrects the term 2 fee. Before she stages anything, git diff shows what she changed:

Terminal
sed -i.bak 's/Term 2 fee: $12,000/Term 2 fee: $12,500/' fees.txt
rm fees.txt.bak
git diff
Output
diff --git a/fees.txt b/fees.txt
index 88cf258..86771b3 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,4 +1,4 @@
 Term 1 fee: $12,000
-Term 2 fee: $12,000
+Term 2 fee: $12,500
 Term 3 fee: $12,000
 Late fee: $250

sed only edits the line, as you would in your editor. The sed line includes .bak so that the same command works on Linux, macOS and in Git Bash on Windows, and rm deletes the backup it makes.

How it works

  1. diff --git a/fees.txt b/fees.txt names the file. a/ is the old version and b/ is the new one.
  2. The @@ line says where in the file the change is. Here it covers lines 1 to 4.
  3. The line starting with - was removed, and the line starting with + replaced it. A changed line always appears as a removal followed by an addition.
  4. Lines starting with a space are unchanged. They are shown so that you can see where the change is.

The lesson "How to Read a Diff" goes through every part of this output in detail.

What Will I Commit? git diff --staged

Navika stages the change and runs git diff again:

Terminal
git add fees.txt
git diff
git status -s
Output
M  fees.txt

git diff printed nothing at all. The only line comes from git status -s, and it shows that the change didn't disappear: M means it is staged. Plain git diff compares the working tree with the staging area, and now they are the same. To see what is staged, that is, what the next commit will contain, add --staged:

Terminal
git diff --staged
Output
diff --git a/fees.txt b/fees.txt
index 88cf258..86771b3 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,4 +1,4 @@
 Term 1 fee: $12,000
-Term 2 fee: $12,000
+Term 2 fee: $12,500
 Term 3 fee: $12,000
 Late fee: $250

--cached is another name for --staged. They mean exactly the same.

Everything Since the Last Commit: git diff HEAD

Now Navika also edits the README, without staging it. There are two kinds of changes at once:

Terminal
echo "Tracks school fee payments." >> README.md
git status -s
git diff --stat
git diff --staged --stat
git diff HEAD --stat
Output
 M README.md
M  fees.txt
 README.md | 1 +
 1 file changed, 1 insertion(+)
 fees.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 README.md | 1 +
 fees.txt  | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

--stat shows each comparison as a short list of files, which makes the difference clear:

CommandComparesHere it shows
git diffWorking tree with the staging areaREADME.md, the change not staged yet
git diff --stagedStaging area with the last commitfees.txt, the change that will be committed
git diff HEADWorking tree with the last commitBoth: everything since the last commit
Which two places each form of git diff compares

Syntax

Terminal
git diff [options] [<commit>] [<commit>] [-- <path>...]
FormIt is used to
git diffShow changes not staged yet
git diff --stagedShow changes staged for the next commit
git diff HEADShow all changes since the last commit
git diff <commit>Show changes in the working tree since a commit
git diff <a> <b>Show changes between two commits
git diff ... -- <path>Limit any of these to a file or folder

Navika commits both changes:

Terminal
git commit --quiet -am "Correct the term 2 fee and describe the app"
git log --oneline
Output
e2ef680 (HEAD -> main) Correct the term 2 fee and describe the app
f585e6f Add the late fee
4f1489e Add README and the term fees

Compare Two Commits

Give git diff two commits, and it shows what changed from the first to the second. The older one usually comes first:

Terminal
git diff 4f1489e HEAD
Output
diff --git a/README.md b/README.md
index d5924b0..223bab1 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 Fee Tracker
+Tracks school fee payments.
diff --git a/fees.txt b/fees.txt
index 0b683c6..86771b3 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,3 +1,4 @@
 Term 1 fee: $12,000
-Term 2 fee: $12,000
+Term 2 fee: $12,500
 Term 3 fee: $12,000
+Late fee: $250

This is every change since the first commit, across both later commits, as one diff. Swap the two commits and every + becomes a -. Git always shows how to get from the first to the second.

HEAD~1 means "the commit before HEAD", so git diff HEAD~1 HEAD shows the changes of the newest commit only, much like git show.

Make a Diff Easier to Read

Only the file names. --name-only lists the changed files, and --name-status adds a letter: A added, M modified, D deleted.

Terminal
git diff --name-status 4f1489e HEAD
Output
M	README.md
M	fees.txt

Only one file. Put the path after --:

Terminal
git diff 4f1489e HEAD -- README.md
Output
diff --git a/README.md b/README.md
index d5924b0..223bab1 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 Fee Tracker
+Tracks school fee payments.

Changed words instead of whole lines. When a long line changes in one place, a line diff shows the whole line twice. --word-diff marks only the words that changed: [-removed-] and {+added+}:

Terminal
sed -i.bak 's/Late fee: \$250/Late fee: $300/' fees.txt
rm fees.txt.bak
git diff --word-diff
Output
diff --git a/fees.txt b/fees.txt
index 86771b3..086512c 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,4 +1,4 @@
Term 1 fee: $12,000
Term 2 fee: $12,500
Term 3 fee: $12,000
Late fee: [-$250-]{+$300+}

In a terminal with colors, --color-words shows the same thing in red and green, without the brackets. Both are very useful for text files such as documentation.

Check for whitespace problems. --check warns about spaces at the end of lines, which are invisible in an editor but show up in every diff:

Terminal
echo 'Exam fee: $800 ' >> fees.txt
git diff --check
Output
fees.txt:5: trailing whitespace.
+Exam fee: $800

Run it before you commit. It prints nothing when there is nothing to fix.

In Practice

Most developers run git diff and git diff --staged before every commit. It is the last chance to notice a debugging line, a password or a change you didn't mean to make. If you prefer a side-by-side view, git difftool opens the same comparison in a visual tool, and IntelliJ IDEA, VS Code and the GitHub, GitLab and Bitbucket websites all show diffs side by side.

Common Mistakes

Thinking an empty git diff means nothing changed. It means nothing is unstaged. Check git diff --staged too, or git status.

Expecting git diff to show new files. An untracked file isn't compared with anything, so it doesn't appear. Once it is staged, git diff --staged shows it.

Putting the commits in the wrong order. git diff new old shows every change reversed. Put the older commit first.

Getting stuck in the pager. A long diff opens in a viewer. Press q to leave.

Leaving out the -- before a path. With a name that could also be a branch, Git can't tell what you mean. -- says that what follows are paths.

Under the Hood

Git doesn't store diffs. Each commit holds a full snapshot, and the staging area holds one too. Every time you run git diff, Git compares two snapshots and works out the shortest set of line removals and additions that turns one into the other. That is why the same command can compare any two things: two commits, a commit and the staging area, or the staging area and your folder.

Git can use different methods to find that set of changes. The default, called myers, is fast. --diff-algorithm=histogram often gives a cleaner diff for code where blocks were moved or repeated. Both describe the same change, only grouped differently.

Interview Corner

What is the difference between git diff and git diff --staged?

git diff compares the working tree with the staging area, so it shows changes you have not staged yet. git diff --staged (also --cached) compares the staging area with the last commit, so it shows what the next commit will contain.

How do you see all changes since the last commit, staged or not?

git diff HEAD. It compares the working tree with the last commit.

How do you see the differences between two commits?

git diff <older> <newer>, optionally followed by -- <path> to limit it to some files. --stat or --name-status give a summary instead of the full changes.

What is the difference between git diff a..b and git diff a...b?

a..b is the same as a b: it compares the two commits directly. a...b compares b with the commit where a and b last had a common ancestor, so it shows only what changed on b's side. It is the diff you see in a pull request.

Why doesn't git diff show a new file?

A new file is untracked, so there is nothing in the staging area to compare it with. Stage it and use git diff --staged, or use git add -N <file> (intent to add) so that git diff includes it.

Practice

Challenge: three diffs. In the same repository, the late fee change and the exam fee line are still unstaged. Stage fees.txt, then add the line Built by Navika. to README.md without staging it. Print the short status, then the names of the files in each of the three diffs.

What does Git print? Before you open the solution, decide which file each of git diff, git diff --staged and git diff HEAD will name.

Show solution
Terminal
git add fees.txt
echo "Built by Navika." >> README.md
git status -s
git diff --name-only
git diff --staged --name-only
git diff HEAD --name-only
Output
 M README.md
M  fees.txt
README.md
fees.txt
README.md
fees.txt

git diff names only README.md, the unstaged change. git diff --staged names only fees.txt, which is staged. git diff HEAD names both, because it compares your folder with the last commit.

Checkpoint. In a project of your own, make one change and stage it, then make a second change and don't. Predict, then check, what each of the three forms of git diff shows.

Quick check: Git diff

4 questions
1What does plain git diff show?
2Which command shows what the next commit will contain?
3You staged everything, and git diff prints nothing. What does that mean?
4Which option marks only the changed words in a line?

Key Takeaways

Quick reference

CommandIt is used to
git diffShow unstaged changes
git diff --stagedShow staged changes
git diff HEADShow all changes since the last commit
git diff <a> <b>Compare two commits
git diff --statSummarize changes per file
git diff --name-status <a> <b>List changed files with A, M, D
git diff <a> <b> -- <path>Compare one file between two commits
git diff --word-diffMark changed words instead of lines
git diff --checkFind trailing spaces before committing

Was this tutorial helpful?

Lesson 19 of 106