Lesson Completion
Back to course

Comparing Branches

Beginner
9 minutes4.7Git

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:

SyntaxMeaningUse Case
A..BCommits in B that are NOT in A"What's new in the feature branch?"
A...BCommits 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

flowchart LR subgraph MAIN["main"] A["A"] --> B["B"] --> C["C"] end subgraph FEAT["feature"] B --> D["D"] --> E["E"] end C -->|"git diff main..feature"| DIFF["📝 Line differences"] E -->|"git log main..feature"| LOG["📋 Commits D, E"] style MAIN fill:#1a1a2e,stroke:#e94560,color:#e94560 style FEAT fill:#0f3460,stroke:#53d8fb,color:#53d8fb style DIFF fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style LOG fill:#1a1a2e,stroke:#ffd700,color:#ffd700

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 main before deploying.

Code

bash
# ─── 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..e4f5g6h

Comparison Cheat Sheet

GoalCommand
Commits in feature not in maingit log main..feature --oneline
File differences between branchesgit diff main..feature
Just filenamesgit diff main..feature --name-only
Summary statsgit diff main..feature --stat
How far ahead/behindgit rev-list --left-right --count main...feature
Visual graphgit log --oneline --graph --all

Key Takeaways

  • Use git log A..B to see commits in B not in A.
  • Use git diff A..B to see file changes between branches.
  • Always compare before merging — it prevents surprises.
  • --name-only and --stat give quick overviews without full diffs.

Interview Prep

  • Q: What does git log main..feature show? A: It shows commits that exist in feature but not in main — 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..B means "commits reachable from B but not from A" (one-directional). A...B means "commits reachable from either A or B but not from both" (symmetric difference — useful for seeing all divergence).

Topics Covered

Git BranchingGit Fundamentals

Tags

#git#compare#diff#beginner-friendly

Last Updated

2026-02-12