Git Diff: Comparing Changes
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:
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 --onelinef585e6f (HEAD -> main) Add the late fee
4f1489e Add README and the term feesWhat Have I Changed? git diff
Navika corrects the term 2 fee. Before she stages anything, git diff shows what she changed:
sed -i.bak 's/Term 2 fee: $12,000/Term 2 fee: $12,500/' fees.txt
rm fees.txt.bak
git diffdiff --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: $250sed 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
diff --git a/fees.txt b/fees.txtnames the file.a/is the old version andb/is the new one.- The
@@line says where in the file the change is. Here it covers lines 1 to 4. - 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. - 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:
git add fees.txt
git diff
git status -sM fees.txtgit 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:
git diff --stageddiff --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:
echo "Tracks school fee payments." >> README.md
git status -s
git diff --stat
git diff --staged --stat
git diff HEAD --stat 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:
| Command | Compares | Here it shows |
|---|---|---|
git diff | Working tree with the staging area | README.md, the change not staged yet |
git diff --staged | Staging area with the last commit | fees.txt, the change that will be committed |
git diff HEAD | Working tree with the last commit | Both: everything since the last commit |
Syntax
git diff [options] [<commit>] [<commit>] [-- <path>...]| Form | It is used to |
|---|---|
git diff | Show changes not staged yet |
git diff --staged | Show changes staged for the next commit |
git diff HEAD | Show 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:
git commit --quiet -am "Correct the term 2 fee and describe the app"
git log --onelinee2ef680 (HEAD -> main) Correct the term 2 fee and describe the app
f585e6f Add the late fee
4f1489e Add README and the term feesCompare Two Commits
Give git diff two commits, and it shows what changed from the first to the second. The older one usually comes first:
git diff 4f1489e HEADdiff --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: $250This 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.
git diff --name-status 4f1489e HEADM README.md
M fees.txtOnly one file. Put the path after --:
git diff 4f1489e HEAD -- README.mddiff --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+}:
sed -i.bak 's/Late fee: \$250/Late fee: $300/' fees.txt
rm fees.txt.bak
git diff --word-diffdiff --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:
echo 'Exam fee: $800 ' >> fees.txt
git diff --checkfees.txt:5: trailing whitespace.
+Exam fee: $800Run 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
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 M README.md
M fees.txt
README.md
fees.txt
README.md
fees.txtgit 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.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff HEAD | Show all changes since the last commit |
git diff <a> <b> | Compare two commits |
git diff --stat | Summarize 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-diff | Mark changed words instead of lines |
git diff --check | Find trailing spaces before committing |
Was this tutorial helpful?