Git diff: See Exactly What Changed, Line by Line
Before you commit, you should know exactly what you are committing. git diff shows changes line by line, and it is the habit that separates careful commits from "oops, I committed my debug prints".
Which changes?
git diff compares two states, and the arguments decide which:
git diffwith no arguments: working directory versus the staging area. These are changes you have not staged yet.git diff --staged(same as--cached): the staging area versus the last commit. This is exactly what will be committed.git diff HEAD: everything that changed since the last commit, staged or not.git diff abc1234 def5678: two commits.git diff HEAD~2 HEADshows what the last two commits changed together.
Add a file path at the end (git diff -- notes.txt) to limit the output to one file.
Reading the output
A diff is made of a short header and one or more hunks:
--- a/fileand+++ b/filename the old and new versions.@@ -1,3 +1,4 @@says the hunk starts at line 1 and covers 3 lines in the old file and 4 in the new.- Lines starting with
-were removed, lines starting with+were added, and lines starting with a space are unchanged context to help you find your place.
A changed line appears as a removal followed by an addition.
git init -q
printf 'eggs\nmilk\nbread\n' > list.txt
git add list.txt
git commit -q -m "Shopping list"
printf 'eggs\noat milk\nbread\ncoffee\n' > list.txt
git diff
git diff --staged
git add list.txt
git diff
git diff --staged --stat Transcript (verified by our test suite in a fresh temp folder)
$ git init -q $ printf 'eggs\nmilk\nbread\n' > list.txt $ git add list.txt $ git commit -q -m "Shopping list" $ printf 'eggs\noat milk\nbread\ncoffee\n' > list.txt $ git diff diff --git a/list.txt b/list.txt index f43d6e7..54b312c 100644 --- a/list.txt +++ b/list.txt @@ -1,3 +1,4 @@ eggs -milk +oat milk bread +coffee $ git diff --staged $ git add list.txt $ git diff $ git diff --staged --stat list.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
The transcript commits a short shopping list, edits it, and runs git diff before and after staging. Before git add, plain git diff shows the change and --staged shows nothing. After git add, it is the other way around. That flip is the clearest way to understand the staging area.
Summaries
git diff --stat replaces the line-by-line detail with one line per file and a bar of plus and minus signs, which is useful in large changes. git diff --word-diff highlights changed words inside a line, handy for prose and documentation.
A review routine
Before every commit:
git statusto see which files changed.git diffto review unstaged work, thengit addwhat belongs together.git diff --stagedfor a last look at the commit you are about to make.
Common mistakes
- Running plain
git diffafter staging and thinking there are no changes. Use--staged. - Reading the
-and+lines backwards:-is the old version,+the new.
Summary
git diffis unstaged work;git diff --stagedis the next commit.- Hunks show removed (
-), added (+) and context lines. --statsummarizes; review before every commit.
Quiz: Comparing changes