0
0
GitHow-ToBeginner · 3 min read

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 between branch1 and branch2.
  • git log branch1..branch2: Lists commits in branch2 that are not in branch1.

The double dot .. means "compare from branch1 to branch2".

bash
git diff branch1..branch2
git log branch1..branch2
💻

Example

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 --oneline
Output
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..branch2 shows changes from branch1 to branch2.
  • 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

CommandPurpose
git diff branch1..branch2Show code differences between two branches
git log branch1..branch2Show commits in branch2 not in branch1
git diff branch1...branch2Show changes from the common ancestor to branch2 (three dots)
git fetchUpdate 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.