git diff — Seeing What Changed
The git diff command shows exactly what has changed in your files. It compares different versions of your content line by line, so you can review your work before committing.
Two Types of Diff
Working directory vs. staging area
Running git diff with no arguments shows changes in your working directory that have not yet been staged:
git diff
Staging area vs. last commit
To see what you've already staged (what will go into your next commit), use:
git diff --staged
This is sometimes written as git diff --cached — they mean the same thing.
Reading the Output
A typical diff looks like this:
diff --git a/file.txt b/file.txt
index 83db48f..bf269f4 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
line one
-line two
+line two updated
+line three
line four
- Lines starting with
-were removed (shown in red) - Lines starting with
+were added (shown in green) - Lines with no prefix are unchanged context lines
Try it!
The repository already has a committed file. Modify it and explore diffs:
- Edit the file:
echo "updated content" > file.txt - View the unstaged diff:
git diff - Stage the change:
git add file.txt - Verify no unstaged diff remains:
git diff(should be empty) - View the staged diff:
git diff --staged - Commit:
git commit -m "Update file content" - Confirm everything is clean:
git diffandgit diff --staged(both empty)
Notice how the diff "moves" from git diff to git diff --staged after you run git add.
Comparing Specific Files
You can limit the diff to a single file:
git diff file.txt
git diff --staged file.txt
This is helpful when you've changed many files but only want to review one.