How to Compare Two Git Branches
In this tutorial, you'll learn about How to Compare Two Git Branches. We cover key concepts, practical examples, and best practices.
The Problem
You need to see what changed between two Git branches before merging. Running git diff without the right syntax shows the wrong comparison or too much information.
Quick Fix
Step 1: Compare the tips of two branches
Show the diff between the latest commits on each branch:
git diff main..feature/login
This shows what is in feature/login that is not in main.
Step 2: Show only changes on the feature branch
Use three-dot syntax to compare the merge-base to the feature tip:
git diff main...feature/login
This ignores commits that exist on main but not on feature/login, showing only the actual changes made on the feature branch.
Step 3: List files that differ between branches
See which files changed without the full diff:
git diff --name-only main..feature/login
src/auth/login.ts
src/auth/login.css
src/types/auth.ts
Step 4: Show commit differences with git log
View the commits that are in one branch but not the other:
git log main..feature/login --oneline
a1b2c3d Add login form validation
e5f6g7h Create login API endpoint
i8j9k0l Add login page component
Step 5: Generate a patch file
Save the diff as a patch to apply elsewhere:
git diff main..feature/login > feature.patch
Apply the patch on another branch:
git checkout main
git apply feature.patch
Step 6: Use a visual difftool
Launch a graphical diff viewer:
git difftool main..feature/login
This opens your configured difftool (such as vimdiff, meld, or kdiff3).
Alternative Solutions
Use git range-diff for patch comparison
Compare two patch series with range-diff:
git range-diff main..feature-v1 main..feature-v2
Use a visual GUI tool
Launch GitKraken, Sourcetree, or gitg for branch comparison:
gitk main..feature/login
Common Mistakes to Avoid
Using single-dot instead of double-dot syntax incorrectly. main..branch shows commits in branch not in main. branch..main shows the opposite.
Forgetting the three-dot syntax for feature branches. main...branch shows only changes on the feature branch, excluding shared history.
Comparing branches that have diverged significantly. The diff output may be too large to review. Compare file-by-file with --name-only first.
Pro Tips
Use git diff --stat for a summary. Before diving into full diffs, see which files changed and the number of additions and deletions: git diff --stat main..branch.
Use git difftool with a GUI. Configure a diff tool like Meld or Beyond Compare: git config --global diff.tool meld.
Use git range-diff for rebase verification. After rebasing, compare the old and new branch versions: git range-diff origin/main...HEAD.
Prevention
- Use
git diff main...branch(three dots) when reviewing feature branches to show only branch-specific changes. - Run
git diff --name-onlyfirst to get an overview before diving into the full diff. - Review branch differences before every merge to catch unintended changes early.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro