title: git: programmatically know by how much the branch is ahead/behind a remote branch
body: <p>I would like to extract the information that is printed after a <code>git status</code>, which looks like:</p>
  
  <pre><code># On branch master
  # Your branch is ahead of 'origin/master' by 2 commits.
  </code></pre>
  
  <p>Of course I can parse the output of <code>git status</code> but this is not recommended since this human readable output is liable to change.</p>
  
  <p>There are two problems:</p>
  
  <ol>
  <li>How to know the remote tracked branch? It is often <code>origin/branch</code> but need not be.</li>
  <li>How to get the numbers? How to know whether it is ahead/behind? By how many commits? And what about the diverged branch case?</li>
  </ol>