How to See Branch Differences in Git: Simple Commands
Use the
git diff branch1..branch2 command to see code differences between two branches. To view commit differences, use git log branch1..branch2. These commands help you understand what changed between branches.Syntax
The basic syntax to see differences between two branches is:
git diff branch1..branch2: Shows code changes betweenbranch1andbranch2.git log branch1..branch2: Lists commits inbranch2that are not inbranch1.
The double dot .. means "compare from branch1 to branch2".
bash
git diff branch1..branch2
git log branch1..branch2Example
This example shows how to compare the main branch with a feature branch called feature. It displays the code differences and the commits that exist only in feature.
bash
git diff main..feature
git log main..feature --onelineOutput
diff --git a/app.js b/app.js
index 83db48f..bf12e3a 100644
--- a/app.js
+++ b/app.js
@@ -1,4 +1,5 @@
console.log('Hello World');
+console.log('New feature added');
f3a1b2c Add new feature logging
9d8c7e1 Fix typo in README
Common Pitfalls
Common mistakes when checking branch differences include:
- Using a single dot
.instead of double dots.., which changes the meaning. - Confusing the order of branches;
git diff branch1..branch2shows changes frombranch1tobranch2. - Not fetching latest changes before comparing, leading to outdated results.
bash
git diff main.feature # Wrong: single dot used # Correct: git diff main..feature
Quick Reference
| Command | Purpose |
|---|---|
| git diff branch1..branch2 | Show code differences between two branches |
| git log branch1..branch2 | Show commits in branch2 not in branch1 |
| git diff branch1...branch2 | Show changes from the common ancestor to branch2 (three dots) |
| git fetch | Update local branches with remote changes before comparing |
Key Takeaways
Use
git diff branch1..branch2 to see code changes between branches.Use
git log branch1..branch2 to list commits unique to the second branch.Always fetch latest changes with
git fetch before comparing branches.Remember the order of branches matters in the comparison commands.
Avoid using a single dot
. instead of double dots .. for comparisons.