How do I see the commit differences between branches in git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you want to compare branches in Git, you need to decide whether you care about commit history, file content, or both. For commit-level differences, git log with branch ranges is usually the best tool; for line-by-line code changes, git diff is better.
Most confusion comes from using the right command with the wrong comparison syntax. Once you understand branch ranges, Git becomes much more predictable.
Show Commits in One Branch but Not the Other
To see commits that are in feature but not in main, run:
Read this as “show me commits reachable from feature that are not reachable from main.” This is the most common answer when someone asks for commit differences between two branches.
To reverse the question, swap the branches:
That gives the opposite set.
See Both Sides at Once with --left-right
If you want one report showing commits unique to each branch, use the symmetric difference form with three dots.
Git marks which side each commit comes from. This is useful before a merge because it shows both branch histories relative to their merge base.
Compare the Code Changes, Not Just the Commit List
If the question is really “what content changed between these branches?” then use git diff.
That prints the patch difference between the tips of the two branches.
A narrower summary is often easier to read first:
Use git log to understand history. Use git diff to understand the resulting code difference.
Use the Merge Base When You Want Review-Style Output
A common review scenario is: “show me what feature introduced since it diverged from main.” The three-dot form is often what you want there.
This compares the merge base against feature, which is closer to what code review tools show for a pull request.
That is different from main..feature, which compares the two branch tips directly.
A Small Example
Suppose:
- '
mainhas commitsA -> B -> C' - '
featurebranched fromBand addedD -> E'
Then:
shows D and E.
And:
shows the content changes introduced by feature since the split point at B.
Useful Add-Ons
These flags help in practice:
That makes the branch relationship easier to read.
If you need only commit subjects that mention a topic:
You can also compare specific files:
Common Pitfalls
- Using
git diffwhen you actually want a list of commits, orgit logwhen you actually want file changes. - Confusing
..and.... They answer different questions. - Forgetting which direction the range is written in, which reverses the meaning of the output.
- Comparing branch tips directly when a merge-base comparison would better match code review intent.
- Assuming commit differences and content differences are the same thing. Rebases and cherry-picks can make them diverge.
Summary
- Use
git log branch1..branch2to see commits inbranch2but not inbranch1. - Use
git log --left-right branch1...branch2to see both sides relative to the merge base. - Use
git diffwhen you care about code changes rather than commit objects. - Three dots are often the better choice for review-style comparisons.
- Always decide first whether you want history, content, or both.
Related reading
- How do I set GIT_SSL_NO_VERIFY for specific repos only?
- How do I set up a Kafka service on gitlab-ci.yml?
- How do I show my global Git configuration?
- How do I show the changes which have been staged?
- How do I squash my last N commits together?
- How do I squash two non-consecutive commits?
- How do I stash only one file out of multiple files that have changed?
- How do I sync the SVN revision number with my ASP.NET web site?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.