git ahead/behind info between master and branch?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To see whether one branch is ahead of or behind another in Git, compare the commits reachable from each branch tip. The cleanest command for this is usually git rev-list --left-right --count, and you should fetch first if you care about remote state. Once you separate local branch comparison from remote-tracking comparison, the numbers become much easier to interpret.
Fetch Before Comparing Remote State
If you want meaningful ahead and behind information against origin/master or another remote-tracking branch, update your refs first:
Without a fetch, your local view of the remote may be stale, and the ahead or behind numbers can be misleading.
Use git rev-list --left-right --count
The most direct comparison command is:
This prints two numbers:
- commits only on
master - commits only on
feature
For example, output like this:
means:
- '
masterhas 3 commits not infeature' - '
featurehas 5 commits not inmaster'
That is the most literal ahead and behind view between two branch tips.
Compare Against a Remote-Tracking Branch
A more common real-world check is whether your feature branch differs from the remote main branch:
This tells you how your local feature branch compares with the current remote-tracking origin/master reference.
If you want to know whether your local branch is ahead of or behind its upstream, git status and git branch -vv are also helpful, but they are less general than rev-list for arbitrary branch pairs.
See the Actual Commits Behind the Numbers
Counts are useful, but sometimes you also want to inspect which commits are different:
Commits prefixed on the left belong only to one side, and commits prefixed on the right belong only to the other side. This is the easiest next step when the numeric counts tell you there is a difference but you need to see exactly what it is.
A Quick Status Shortcut
If the branch has an upstream configured, Git can summarize divergence directly:
Those commands are useful for daily workflow, but they are not a full replacement for git rev-list --left-right --count when you want to compare any two arbitrary refs.
Why the Triple-Dot Matters
The command uses master...feature, not master..feature.
That distinction matters:
- double-dot is a one-sided difference view
- triple-dot compares both sides relative to the merge base
For ahead and behind counts, triple-dot is the right tool because you want the unique commits on both branches, not just one direction.
Common Pitfalls
One common mistake is comparing against origin/master without running git fetch first. The numbers may be correct relative to stale refs, but not relative to the real remote state.
Another mistake is confusing local master with origin/master. They are different refs and can diverge.
Developers also sometimes use .. instead of ... and then wonder why the result only shows one side of the difference.
Finally, counts alone do not tell you whether the branches are safe to merge. They only tell you how many unique commits exist on each side.
Summary
- Use
git rev-list --left-right --count branchA...branchBfor exact ahead and behind counts. - Fetch first when remote-tracking refs are involved.
- Compare local branches and remote-tracking branches deliberately, not interchangeably.
- Use
git log --left-rightwhen you need the actual commit list behind the counts. - The triple-dot form is the right syntax for two-sided divergence checks.
Related reading
- Git alias with positional parameters
- Git and Mercurial - Compare and Contrast
- Git and nasty error cannot lock existing info/refs fatal
- git apply changes from one commit onto another branch
- git apply fails with patch does not apply error
- git archive fatal Operation not supported by protocol
- Git as mercurial client? Why no git-hg?
- Git asks for username every time I push
.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.