The Hook (The "Byte-Sized" Intro)
Never merge blind. Before combining two branches, you should know exactly what's different — which files changed, which commits are new, and how many lines were added or removed. git diff and git log with the .. operator give you that x-ray vision. Merging without comparing first is like signing a contract without reading it.
📖 What is Comparing Branches?
Comparing branches means seeing the differences between two branches before merging, reviewing, or deploying. Git provides two main tools: git diff (file-level changes) and git log (commit-level differences).
Conceptual Clarity
Two key operators:
| Syntax | Meaning | Use Case |
|---|---|---|
A..B | Commits in B that are NOT in A | "What's new in the feature branch?" |
A...B | Commits in either A or B but not both | "What diverged since they split?" |
Two comparison tools:
git diff main..feature— shows file-level changes (line-by-line diff)git log main..feature— shows commit-level differences (list of commits)
Real-Life Analogy
Comparing branches is like looking at two versions of a report:
git diff= Showing tracked changes (red/green highlighting of what's different)git log= Showing the revision history (who changed what, when, and why)
Visual Architecture
Why It Matters
- Pre-merge review: See exactly what a merge will bring in before you run it.
- PR preparation: Understand the scope of your changes before opening a pull request.
- Conflict prediction: Large diffs to shared files hint at potential conflicts.
- Deployment safety: Compare the release branch to
mainbefore deploying.
Code
# ─── See what commits are in feature but NOT in main ───
git log main..feature/login --oneline
# Output:
# e4f5g6h Add login validation
# d3c2b1a Create login form
# ─── See what commits are in main but NOT in feature ───
git log feature/login..main --oneline
# Output:
# a1b2c3d Fix homepage layout
# ─── See file-level diff between branches ───
git diff main..feature/login
# Shows all line-by-line changes
# ─── Just the file names that differ ───
git diff main..feature/login --name-only
# Output:
# src/login.js
# src/auth.js
# tests/login.test.js
# ─── Stats summary (insertions/deletions per file) ───
git diff main..feature/login --stat
# Output:
# src/login.js | 45 +++++++++++++++
# src/auth.js | 12 +++++
# tests/login.test.js | 30 ++++++++++
# 3 files changed, 87 insertions(+)
# ─── Count the number of commits ahead/behind ───
git rev-list --left-right --count main...feature/login
# Output: 1 2
# Meaning: main has 1 commit ahead, feature has 2 commits ahead
# ─── Compare with a specific commit (not just branch tips) ───
git diff a1b2c3d..e4f5g6hComparison Cheat Sheet
| Goal | Command |
|---|---|
| Commits in feature not in main | git log main..feature --oneline |
| File differences between branches | git diff main..feature |
| Just filenames | git diff main..feature --name-only |
| Summary stats | git diff main..feature --stat |
| How far ahead/behind | git rev-list --left-right --count main...feature |
| Visual graph | git log --oneline --graph --all |
Key Takeaways
- Use
git log A..Bto see commits in B not in A. - Use
git diff A..Bto see file changes between branches. - Always compare before merging — it prevents surprises.
--name-onlyand--statgive quick overviews without full diffs.
Interview Prep
-
Q: What does
git log main..featureshow? A: It shows commits that exist infeaturebut not inmain— essentially, the new work in the feature branch. Reversing the order (feature..main) shows commits in main that feature doesn't have. -
Q: How do you quickly see how many commits a branch is ahead or behind? A: Use
git rev-list --left-right --count main...feature. The output shows two numbers: how many commits main is ahead and how many feature is ahead. -
Q: What is the difference between
..and...in Git comparisons? A:A..Bmeans "commits reachable from B but not from A" (one-directional).A...Bmeans "commits reachable from either A or B but not from both" (symmetric difference — useful for seeing all divergence).