Why Compare Changes
Before you stage or commit, you want to know exactly what changed: which lines
you added, which you removed, and whether anything unintended slipped in. git diff answers that line by line. You used it briefly when splitting a commit;
this lesson explains each form of the command and how to read its output.
Four Comparisons
A diff always compares two versions of your files: a before version and an after version. What changes from one command to the next is which two versions are compared.

Read each row of the diagram from left to right:
git diffcompares the staging area with the working tree. It shows edits you have made but not yet staged: what you could still add.git diff --stagedcompares the last commit with the staging area. It shows what the next commit will record.--cachedis another name for the same option, and you will often see it in documentation and online answers.git diff HEADcompares the last commit with the working tree. It shows everything that changed since the last commit, staged or not.git diff HEAD~1 HEADcompares two commits, here the newest commit and its parent. Any names for commits work: IDs,HEAD~nand, once you meet them in the branching module, branch names.
git diff # staging area -> working tree
git diff --staged # last commit -> staging area
git diff HEAD # last commit -> working tree
git diff HEAD~1 HEAD # older commit -> newer commit
Two additions work with every form. Put -- <file> at the end to compare only
that file, as with git log. Add --stat to see a summary of files and line
counts instead of the lines themselves.
Reading a Diff
Here is the diff for one edit to a recipe file, changing the amount of sugar:
diff --git a/recipe.md b/recipe.md
index 079c31f..40bf57a 100644
--- a/recipe.md
+++ b/recipe.md
@@ -4,5 +4,5 @@ Boil 2 cups of water.
Add 4 crushed cardamom pods.
Add 2 teaspoons of tea leaves.
Add 1 cup of milk.
-Add 1 teaspoon of sugar.
+Add 2 teaspoons of sugar.
Strain into cups.
From the top:
diff --git a/recipe.md b/recipe.mdstarts the section for one file.a/labels the before version andb/the after version.index 079c31f..40bf57a 100644gives short IDs for the stored before and after versions of the file, and its mode:100644is an ordinary file. You can usually skip this line.--- a/recipe.mdand+++ b/recipe.mdsay that lines starting with-come from the before version and lines starting with+from the after version.@@ -4,5 +4,5 @@is a hunk header. A hunk is one block of nearby changes, and a file with changes far apart has several.-4,5means the hunk shows 5 lines of the before version, starting at line 4.+4,5means it shows 5 lines of the after version, also starting at line 4.- Anything after the closing
@@, hereBoil 2 cups of water., is not part of the change. Git repeats an earlier line of the file there to help you find your place. In program code it is usually the name of the function the change is in.
Then come the lines themselves. Each starts with one character:
| First character | Meaning |
|---|---|
| A space | Context: unchanged, shown to help you see where the change is |
- | In the before version only: removed |
+ | In the after version only: added |
A changed line appears as a removal followed by an addition, as the sugar line does here. By default Git shows up to three context lines on each side of a change. In most terminals, removed lines are red and added lines green.
Why a Diff Can Be Empty
git diff printing nothing does not always mean nothing changed. Two common
reasons:
- The changes are staged. Plain
git diffcompares the staging area with the working tree. Once you stage an edit, the two match, so it shows nothing.git diff --stagedshows the change. - The file is new. A diff compares versions Git already has. A new,
untracked file has no version in the staging area or the last commit, so
none of the four forms shows it.
git statuslists it under "Untracked files". Once you add it,git diff --stagedshows it as a new file: the header includesnew file mode, the before side is--- /dev/null, meaning there was no earlier version, and every line starts with+.
Exercise: Predict and Read Diffs
You will put a repository into several states. Before running each command, predict what it will show, then run it and compare.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It makes two commits: a chai recipe, then a change adding cardamom.
mkdir -p ~/git-practice/read-diffs cd ~/git-practice/read-diffs git init -b main cat > recipe.md <<'EOF' # Masala chai Boil 2 cups of water. Add 2 teaspoons of tea leaves. Add 1 cup of milk. Add 1 teaspoon of sugar. Strain into cups. EOF git add recipe.md git commit -m "Add chai recipe" cat > recipe.md <<'EOF' # Masala chai Boil 2 cups of water. Add 4 crushed cardamom pods. Add 2 teaspoons of tea leaves. Add 1 cup of milk. Add 1 teaspoon of sugar. Strain into cups. EOF git commit -am "Add cardamom" -
Compare the two commits. Predict: which line will start with
+, and will any line start with-?git log --oneline git diff HEAD~1 HEAD git diff HEAD~1 HEAD --statThe cardamom line is the only
+line, and no line starts with-. The hunk header is@@ -1,6 +1,7 @@: 6 lines of the older commit, 7 of the newer one.--statreports1 file changed, 1 insertion(+). -
Make an unstaged edit. Change the sugar line:
cat > recipe.md <<'EOF' # Masala chai Boil 2 cups of water. Add 4 crushed cardamom pods. Add 2 teaspoons of tea leaves. Add 1 cup of milk. Add 2 teaspoons of sugar. Strain into cups. EOFPredict what each of these shows, then run them:
git diff git diff --stagedgit diffshows the hunk from the Reading a Diff section above: the old sugar line as-, the new one as+, with four context lines.git diff --stagedprints nothing, because nothing is staged yet. -
Stage the edit, then predict both commands again:
git add recipe.md git diff git diff --stagedNow the results swap. Plain
git diffgoes quiet because the staging area and the working tree match.git diff --stagedshows the sugar change, because that is what the next commit would record. -
Edit again after staging. Add a final line, then predict three commands:
cat >> recipe.md <<'EOF' Serve hot. EOF git diff git diff --staged git diff HEADEach shows something different:
git diffshows only+Serve hot., the edit made after staging.git diff --stagedstill shows only the sugar change.git diff HEADshows both, because it compares the last commit with the working tree:
@@ -4,5 +4,6 @@ Boil 2 cups of water. Add 4 crushed cardamom pods. Add 2 teaspoons of tea leaves. Add 1 cup of milk. -Add 1 teaspoon of sugar. +Add 2 teaspoons of sugar. Strain into cups. +Serve hot. -
Create a new file. Predict whether
git diffwill mention it:cat > shopping.md <<'EOF' # Shopping Cardamom pods EOF git diff git statusgit diffshows exactly what it showed in step 5:shopping.mdis untracked, so no diff includes it.git statuslists it under "Untracked files". It also listsrecipe.mdtwice, under "Changes to be committed" and "Changes not staged for commit", because it has both kinds of change.
Success: your predictions matched in each step, or you can say why they did
not. In particular: plain git diff was empty in step 4, the three commands in
step 5 showed three different results, and shopping.md appeared only in
git status.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
git diff prints nothing, but git status lists a modified file | The change is staged | Run git diff --staged |
| A new file is missing from every diff | The file is untracked | Check git status; after git add, git diff --staged shows it |
A : or (END) at the bottom of the screen | Git is showing output one page at a time | Press Space to scroll or q to quit |
unknown revision or path not in the working tree for HEAD~1 | A setup commit failed, so only one commit exists | Run git log --oneline; if one commit is missing, remove the folder and run the setup again |
\ No newline at end of file under a line | That file's last line has no line ending, which some editors allow | Harmless; the exercise commands always end the last line |
-
Clean up:
cd ~/git-practice ls rm -rf read-diffs ls
Independent task: before cleaning up, continue from step 6:
- Run
git add shopping.md. Before runninggit diff --staged, predict which files it will show and whetherServe hot.will be in it. Then check. - In step 5,
git diffprinted the hunk header@@ -6,3 +6,4 @@. What do its four numbers tell you? - Which single command now shows every change since the last commit,
including
shopping.md?
Check Your Understanding
- You edit a tracked file
a.txt, rungit add a.txt, then create a new fileb.txt. What dogit diff,git diff --stagedandgit diff HEADeach show? - A hunk header reads
@@ -10,4 +10,6 @@. What does it tell you about the before and after versions?
Answer and Reasoning
Independent task
git diff --stagedshows two files.recipe.mdhas only the sugar change, andshopping.mdappears as a new file:new file mode 100644, a before side of--- /dev/nulland three+lines.Serve hot.is not there: you added that line after stagingrecipe.md, so it is still an unstaged edit that onlygit diffshows.- The hunk shows 3 lines of the before version starting at line 6, and 4 lines
of the after version starting at line 6. The after side has one more line:
the added
Serve hot.. The text Git printed after the header is an earlier line of the file, not part of the change. git diff HEAD, which compares the last commit with the working tree. It shows the sugar change,Serve hot.andshopping.md. The new file appears now because it has been added; beforegit add, no form ofgit diffshowed it.
Understanding check
git diffshows nothing: the edit toa.txtis staged, andb.txtis untracked.git diff --stagedshows thea.txtedit.git diff HEADalso shows thea.txtedit.b.txtappears in none of them until it is added;git statusis where you see it.- The hunk covers 4 lines of the before version and 6 lines of the after version, both starting at line 10. The after version has two more lines in this hunk: for example, two added lines, or three added and one removed.