Module 3 Lesson 5: Diffing changes
·DevOps

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).

  1. The Mistake: Create a new file database.js and add a fake database connection string. Stage it.
  2. The Verification: Run git diff --staged. Ensure the connection string is correct.
  3. The Commit: Commit the change with a professional message.
  4. The Audit: Run git log --oneline and find your new commit ID.
  5. The Change: Edit database.js to add a comment. Run git diff to see your local edit.
  6. The Reversion: You decide the comment was unnecessary. Use git restore database.js to discard it.
  7. The Inspection: Use git show HEAD to 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 diff shows unstaged changes.
  • git diff --staged shows 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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn