git
version control
branch comparison
git commands
software development

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.

Browse interview questions

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:

bash
git fetch origin

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:

bash
git rev-list --left-right --count master...feature

This prints two numbers:

  • commits only on master
  • commits only on feature

For example, output like this:

text
3	5

means:

  • 'master has 3 commits not in feature'
  • 'feature has 5 commits not in master'

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:

bash
git fetch origin
git rev-list --left-right --count origin/master...feature

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:

bash
git log --oneline --left-right master...feature

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:

bash
git status
git branch -vv

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...branchB for 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-right when you need the actual commit list behind the counts.
  • The triple-dot form is the right syntax for two-sided divergence checks.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.