Tour of Git
Git Basics / Lesson 1.7

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:

  1. Edit the file: echo "updated content" > file.txt
  2. View the unstaged diff: git diff
  3. Stage the change: git add file.txt
  4. Verify no unstaged diff remains: git diff (should be empty)
  5. View the staged diff: git diff --staged
  6. Commit: git commit -m "Update file content"
  7. Confirm everything is clean: git diff and git 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.

🎯

Goal

Modify a file and stage the changes after viewing the diff

Terminal
$

No commits yet. Run `git init` and create your first commit.