Git/
Lesson

Code changes constantly. You modify files, stage them, commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. them, branch and merge. At any point, you need to know: what is different? What changed between then and now? Between this branch and that one?

Git's diffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. capability is one of its most powerful features. It can compare any two points in your repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.'s history, or between your current work and what is saved.

How diffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. works

At its core, git diff answers one question: "What is the difference between version A and version B?"

Git shows you this by displaying:

  • Lines that exist in A but not in B (removed, shown with -)
  • Lines that exist in B but not in A (added, shown with +)
  • Context lines around the changes (unchanged, shown without prefix)

02

DiffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. without arguments: unstaged changes

git diff

This compares your working directory against your staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save.. It shows changes you have made but have not staged yet.

Example output

diff --git a/src/auth.js b/src/auth.js
--- a/src/auth.js
+++ b/src/auth.js
@@ -10,7 +10,10 @@ export function loginUser(credentials) {
   if (!credentials.email || !credentials.password) {
     throw new Error('Email and password required');
   }
-
+
+  // Hash the password before validation
+  const hashedPassword = hashPassword(credentials.password);
+
   // Validate against database
   const user = await db.findUser(credentials.email);
SymbolMeaning
--- a/fileThe old version (staging area)
+++ b/fileThe new version (working directory)
- prefixLine was removed
+ prefixLine was added
No prefixContext line (unchanged)
@@ -10,7 +10,10 @@Change location: starts at line 10
03

DiffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. staged changes

What about changes you have already staged?

git diff --staged

This compares your staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save. against the last commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.. It shows what you have staged but have not committed yet.

AI pitfall
AI-generated code often tells you to just git add . && git commit without reviewing first. Before committing, always run git diff --staged to double-check what you are about to commit. This catches accidental additions like debug statements, console.log lines, or sensitive data that should not be committed.
04

Comparing two commits

The real power comes from comparing any two points in history:

# What changed in the last commit?
git diff HEAD~1 HEAD

# What changed between two specific commits?
git diff abc1234 def5678
05

Comparing branches

This is where diffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. becomes incredibly useful for team workflows.

Two-dot diff (direct comparison)

git diff main feature-branch

Shows the cumulative difference between the tips of both branches.

Three-dot diff (since divergence)

git diff main...feature-branch

Shows only the changes made on the feature branch since it diverged from main. It excludes any changes that happened on main after the branch point.

SyntaxWhat it comparesUse case
A BTip of A vs tip of BSee total difference
A...BChanges unique to B since divergenceSee what a branch adds
06

DiffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. options

# Show only filenames that changed
git diff --name-only

# Show statistics (files changed, insertions, deletions)
git diff --stat

# Highlight individual words that changed
git diff --word-diff

# Ignore whitespace changes
git diff -w
07

Quick reference

CommandWhat it compares
git diffWorking directory vs staging area
git diff --stagedStaging area vs last commit
git diff HEADWorking directory vs last commit
git diff abc123 def456Two specific commits
git diff main featureBranch main vs branch feature
git diff main...featureChanges on feature since branching
git diff --name-onlyJust filenames
git diff --statStatistics only
git diff -wIgnore whitespace changes