Git Compare Two Branches | How to See Differences Before Merging
Before merging a feature into main, wouldn't you want to know exactly what changed? Git can show you — before you merge a single line.
You've been on a payment feature for days. Controllers, models, routes. Opening files one at a time to review them is slow and error-prone. git diff compares the content between two Git references and tells you which files changed, and which lines were added or removed.
This 2-minute explainer covers it with a Ruby on Rails payment-api example.
What you'll learn:
- What comparing two branches actually means
- How git diff works, and what a "reference" is
- How added and removed lines are shown
- How to list only the changed filenames
- How to compare a single file
- The difference between git diff and git log
- When to use main..feature versus main...feature
The commands:
# list your branches
git branch
# compare all content between the two branches
git diff main..feature/payment
# just the filenames that differ
git diff --name-only main..feature/payment
# narrow it to one path
git diff main..feature/payment -- app/models/payment.rb
# the commits on the feature branch but not on main
git log --oneline main..feature/payment
Two dots vs three dots — the part most people get wrong:
main..feature/payment compares the two branch tips as they are right now
main...feature/payment compares from the point where the branches diverged
(the merge base) against the feature branch
Three dots is usually what you want when reviewing a feature, because it shows what the feature introduced rather than mixing in everything main has gained since.
And one limit worth stating plainly: git diff does NOT merge your branches. It won't catch a logic bug, and it can't prove the code works. Review first. Merge after.
Chapters:
00:00 — The Hook: Know What Changed
00:10 — The Problem Before Merging
00:25 — What Is Git Diff?
00:40 — How Branch Comparison Works
01:00 — Practical Rails Example
01:25 — Benefits and Limitations
01:45 — The Takeaway
Never merge blindly. Compare first.
#git #github #versioncontrol #gitdiff #developer #programming #softwareengineering #devops #rubyonrails #coding #webdevelopment #gitcommands #codingtips #softwaredevelopment #codereview
— #Astmation —
#software #systems, #automation, and #AI-powered #applications.
Build. Automate. Evolve.
https://astmation.com/
Watch on YouTube