Looking back and undoing · lesson 2 of 7
Commands and ideas in this lesson
Agit diff A Bwhat changed from A to B
B@@ -10,7 +10,7 @@a hunk header: where the change is
C--word-diffchanged words, not whole lines

Comparing versions

Last updated

git diff compares any two versions: your folder and the last commit, two commits, or one file then and now. In this lesson you'll read a diff line by line, compare across history, and use word diffs for writing, where a whole-line diff hides a one-word change.

By the end of this lesson you can

  • Compare two commits, or one file at two commits, with git diff
  • Read a diff: headers, hunks, context, + and - lines
  • Use --word-diff for prose
  • Know that swapping the two versions swaps + and -

What git diff compares

git diff always compares two versions and shows what changed from the first to the second:

CommandFromTo
git diffthe staging areayour working folder (unstaged changes)
git diff --stagedthe last committhe staging area (what you'd commit)
git diff HEADthe last commityour working folder (all uncommitted changes)
git diff A Bcommit Acommit B
git diff A B -- adobo.mdadobo.md at Aadobo.md at B

A and B can be any names from lesson 2.1: hashes, HEAD~3, and from Stage 3, branch names.

Reading a diff

git diff HEAD~6 HEAD -- adobo.md
git diff HEAD~6 HEAD -- adobo.md
diff --git a/adobo.md b/adobo.md
index 47d555c..e0ae414 100644
--- a/adobo.md
+++ b/adobo.md
@@ -10,7 +10,7 @@
 - 1 head garlic, crushed
 - 3 bay leaves
 - 1 tsp whole peppercorns
-- a little salt
+- 1 tsp salt
 
 ## Steps
 

Line by line:

  • diff --git a/adobo.md b/adobo.md: the file. a/ is the first version, b/ the second.
  • index 47d555c..e0ae414: the short hashes of the file's two contents (Stage 8 explains these).
  • --- and +++: which side is which. Lines from a get -, lines from b get +.
  • @@ -10,7 +10,7 @@: a hunk header. In a, the hunk starts at line 10 and is 7 lines long; in b, the same.
  • Lines starting with a space are context, unchanged, there to show where you are.
  • -- a little salt is the line - a little salt (a Markdown list item) removed, and +- 1 tsp salt the line - 1 tsp salt added. A changed line always appears as a removal and an addition.

Compare across history

Git sandbox
  1. See every change to the project between the first commit and now: git diff HEAD~9 HEAD --stat
  2. Compare adobo.md then and now: git diff HEAD~9 HEAD -- adobo.md
  3. Compare leche-flan.md between any two commits you choose

Working folder

no changes

Staging area

nothing staged

Repository

10 commits on main

You are in ~/kusina, which has ten commits from two weeks of work.

Commit graph

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

--stat gives a summary first, which is often all you need: which files changed, and roughly how much.

Word diffs for prose

Diffs work line by line. In a recipe or an essay, a "line" can be a long sentence, and changing one word in it shows the whole sentence removed and added again. --word-diff marks just the words: [-removed-] and {+added+}.

Git sandbox
  1. Show Joaquin’s sinigang commit as a normal diff: git show HEAD~3
  2. Show it again with git show --word-diff HEAD~3 and spot the two added phrases
You are in ~/kusina, which has ten commits from two weeks of work.

For writing kept in Git (theses, documentation, recipes), it helps to start each sentence on a new line. Diffs then show changed sentences, and nobody's paragraph shows as completely rewritten.

Spot the trap: the order matters

The same two versions, the other way round
git diff HEAD HEAD~6 -- adobo.md
...
@@ -10,7 +10,7 @@
 - 1 head garlic, crushed
 - 3 bay leaves
 - 1 tsp whole peppercorns
-- 1 tsp salt
+- a little salt
Spot the trapJoaquin

Joaquin runs git diff HEAD HEAD~6 -- adobo.md and tells Ana: “Someone changed 1 tsp salt to a little salt. That’s a step backwards.”

What happens if…

Check what you learned

Which command shows every change you haven’t committed, staged or not?
In @@ -10,7 +10,8 @@, what happened?
Ana changed one word in a long sentence of the README. Which shows it most clearly?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git