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:
| Command | From | To |
|---|---|---|
git diff | the staging area | your working folder (unstaged changes) |
git diff --staged | the last commit | the staging area (what you'd commit) |
git diff HEAD | the last commit | your working folder (all uncommitted changes) |
git diff A B | commit A | commit B |
git diff A B -- adobo.md | adobo.md at A | adobo.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 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 fromaget-, lines frombget+.@@ -10,7 +10,7 @@: a hunk header. Ina, the hunk starts at line 10 and is 7 lines long; inb, the same.- Lines starting with a space are context, unchanged, there to show where you are.
-- a little saltis the line- a little salt(a Markdown list item) removed, and+- 1 tsp saltthe line- 1 tsp saltadded. A changed line always appears as a removal and an addition.
Compare across history
- See every change to the project between the first commit and now:
git diff HEAD~9 HEAD --stat - Compare adobo.md then and now:
git diff HEAD~9 HEAD -- adobo.md - 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
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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+}.
- Show Joaquin’s sinigang commit as a normal diff:
git show HEAD~3 - Show it again with
git show --word-diff HEAD~3and 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
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
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
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.
- Git documentation: git-diff: the forms of git diff, and --word-diff
- Git documentation: diff format (git diff -p): headers, hunks and the unified format
- Pro Git: Viewing Your Staged and Unstaged Changes: git diff and git diff --staged