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)
DiffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. without arguments: unstaged changes
git diffThis 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);| Symbol | Meaning |
|---|---|
--- a/file | The old version (staging area) |
+++ b/file | The new version (working directory) |
- prefix | Line was removed |
+ prefix | Line was added |
| No prefix | Context line (unchanged) |
@@ -10,7 +10,10 @@ | Change location: starts at line 10 |
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 --stagedThis 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.
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.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 def5678Comparing 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-branchShows the cumulative difference between the tips of both branches.
Three-dot diff (since divergence)
git diff main...feature-branchShows 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.
| Syntax | What it compares | Use case |
|---|---|---|
A B | Tip of A vs tip of B | See total difference |
A...B | Changes unique to B since divergence | See what a branch adds |
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 -wQuick reference
| Command | What it compares |
|---|---|
git diff | Working directory vs staging area |
git diff --staged | Staging area vs last commit |
git diff HEAD | Working directory vs last commit |
git diff abc123 def456 | Two specific commits |
git diff main feature | Branch main vs branch feature |
git diff main...feature | Changes on feature since branching |
git diff --name-only | Just filenames |
git diff --stat | Statistics only |
git diff -w | Ignore whitespace changes |