git: programmatically know by how much the branch is ahead/behind a remote branch

Posted by Olivier on Stack Overflow See other posts from Stack Overflow or by Olivier
Published on 2010-06-03T19:43:21Z Indexed on 2010/06/03 21:44 UTC
Read the original article Hit count: 320

Filed under:
|
|

I would like to extract the information that is printed after a github status, which looks like:

# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.

Of course I can parse the output of git status but this is not recommended since this human readable output is liable to change.

There are two problems:

  1. How to know the remote tracked branch? It is often origin/branch but need not be.
  2. How to get the numbers? How to know whether it is ahead/behind? By how many commits? And what about the diverged branch case?

© Stack Overflow or respective owner

Related posts about git

Related posts about programmatically