
Module 3 Lesson 5: Diffing changes
See exactly what changed. Master 'git diff' to compare your working folder against the staging area, and your staging area against your latest commit.
Module 3 Lesson 5: Diffing changes
Before you commit, and even before you stage, you should always ask yourself: "What exactly have I changed?"
The git diff command allows you to compare the differences between any two snapshots, branches, or files in your project. In this lesson, we learn how to read a "diff" and use it to verify our work.
1. Comparing Working Directory and Staging Area
When you run git diff with no arguments, Git shows you the differences between your Current Files (on disk) and your Staging Area (the Index).
git diff
How to Read the Output:
- Red lines starting with
-: Lines that were removed. - Green lines starting with
+: Lines that were added. - White lines: Untouched code surrounding the change (context).
2. Comparing Staging Area and Last Commit
Once you have run git add, the command above will return nothing (because your disk and your staging area now match). To see what you have staged but not yet committed, use the --staged (or --cached) flag:
git diff --staged
Workflow Tip: Use git diff to check your work, then git add, then git diff --staged to do a final "sanity check" before you commit.
3. Comparing Two Commits
You can compare any two points in your history by using their SHA-1 IDs:
# git diff <commit-1> <commit-2>
git diff 1a2b3c4 7f3d9a1
You can also compare your current state against a commit from two versions ago:
git diff HEAD~2
graph LR
WD["Working Directory"] -- "git diff" --> SA["Staging Area"]
SA -- "git diff --staged" --> GD["Git Directory (HEAD)"]
GD -- "git diff HEAD~1" --> P["Previous Commit"]
4. Comparing Branches
Before you merge two features, you might want to see how much they have diverged:
git diff main feature-login
Module 3 Comprehensive Practice Exercises
It’s time to combine everything you’ve learned in Module 3 (Add, Commit, Log, Undo, Diff).
- The Mistake: Create a new file
database.jsand add a fake database connection string. Stage it. - The Verification: Run
git diff --staged. Ensure the connection string is correct. - The Commit: Commit the change with a professional message.
- The Audit: Run
git log --onelineand find your new commit ID. - The Change: Edit
database.jsto add a comment. Rungit diffto see your local edit. - The Reversion: You decide the comment was unnecessary. Use
git restore database.jsto discard it. - The Inspection: Use
git show HEADto see exactly what you did in that last commit.
Checkpoint: If you can explain the difference between git diff and git diff --staged, you have mastered the fundamental Git workflow!
Summary
In this lesson, we established:
git diffshows unstaged changes.git diff --stagedshows what is about to be committed.- You can compare any two commits, pointers, or branches using their names or IDs.
- Reading "diffs" is an essential skill for code reviews and debugging.
Module Complete! You are now a competent Git user for local, single-branch projects.
Next Module: Things are about to get interesting. Welcome to Module 4: Branching and Merging, where we learn how to manage multiple streams of work simultaneously.