Black Friday sale: 30% off Pro and Unlimited, until 22 December

“Your branch is ahead of origin by N commits”: what it means and what to do

Git says your branch is ahead of origin. That means you have commits its last copy of the server branch doesn't. See which ones, then push them or keep them local on purpose.

You ran git status and Git answered:

On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

Nothing is broken. Your main has two commits that origin/main doesn’t have, as far as this repository knows. Those commits may exist only on this machine, where a lost laptop takes them along. Or another clone may have pushed the same commits already: Git doesn’t check the server until you fetch, so it can still say ahead.

What the message compares

Your branch is being compared with its upstream, the branch it is set to track. Here that is origin/main, which is Git’s local copy of main on the server.

That copy only changes when you fetch, pull or push. Git doesn’t ask the server when you run git status, so “ahead by 2” is measured against what your repository last saw. Since then a teammate may have pushed, or the same commits may have reached the server from another clone or under another branch name. Run git fetch first if you want the current picture.

See which commits are ahead

git log --oneline @{u}..HEAD

@{u} is your branch’s upstream, and HEAD is where you are. The range @{u}..HEAD means “commits I have that the upstream doesn’t”.

18fc5d5 Add coupon field
ccd1e6e Fix cart rounding

To see the files each commit touched, add --stat:

git log --stat --format='%h %s' @{u}..HEAD
18fc5d5 Add coupon field

 src/checkout.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ccd1e6e Fix cart rounding

 src/cart.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

For one summary of the changes in those commits:

git diff --stat @{u}...HEAD

The three dots compare from the point where your branch and the upstream split, so only your side shows, even when the upstream has moved on too.

If you only want the number, git rev-list --count @{u}..HEAD prints 2.

For a shorter status line, use git status -sb. Its first line reads ## main...origin/main [ahead 2].

Push them

git push
   9436433..18fc5d5  main -> main

Run git status again and the message is gone: Your branch is up to date with 'origin/main'.

If Git rejects the push with (fetch first) or (non-fast-forward), the server branch has commits you don’t have. Run git fetch, look at what arrived with git log --oneline HEAD..@{u}, then rebase or merge before you push again. If git status already says your branch and origin/main “have diverged”, the same steps apply. Other rejections, such as a protected branch, need a different fix: the reason is in parentheses at the end of the ! [remote rejected] line, and any remote: lines above it say more.

Or keep them local, on purpose

Sometimes ahead is exactly right: a work in progress you aren’t ready to share, or an experiment.

  • Leave them. Ahead is not an error, and nothing forces you to push. Just remember that unless they went somewhere else, the commits live only on this machine.
  • Give them a name of their own. git branch wip puts a new branch on your current commit, so the work keeps a name you can find and push later with git push -u origin wip. It holds committed work only: changes you haven’t committed stay in your working folder, not on wip.

The message only covers the branch you are on

git status speaks for the current branch. Other branches and other repositories can be ahead too, and Git says nothing about them until you check each one. The same question across every branch and repository takes a loop.

Or let your Mac count for you

push& (Pushand) is a Mac menu bar app that shows the ahead count for each branch in the repositories you choose, so you don’t have to run git status in each one. Expand a repository to read the waiting commits and push a branch with a click. Free for 3 repositories, and every install starts with a 14-day trial.

See the features

More Git how-tos