git: programmatically know by how much the branch is ahead/behind a remote branch
- by Olivier
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:
How to know the remote tracked branch? It is often origin/branch but need not be.
How to get the numbers? How to know whether it is ahead/behind? By how many commits? And what about the diverged branch case?