I would like to extract the information that is printed after a git status, which looks like:
# On branch master
# Your branch is ahead of \'origin/
How to know the remote tracked branch? It is often
origin/branchbut need not be.
Git 2.5+ introduces a new shortcut which references the branch you are pushing to. @{push}: that would be the remote tracking branch which is of interest here.
That means you have another option to see ahead/behind for all branches which are configured to push to a branch.
git for-each-ref --format="%(push:track)" refs/heads
See more at "Viewing Unpushed Git Commits"