I can use git status to tell me if I'm ahead or behind the remote:
git status
On branch master
Your branch is ahead of 'bean/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Which is fine for parsing:
needs_push = "Your branch is ahead" in std.out
needs_pull = "Your branch is behind" in std.out
However, when working with multiple remotes things fall down, since git status only shows the result for one remote (bean in the example above) and takes no parameters to select the remote.
Is there any way I to specify the remote to show details for? i.e. given two remotes, does my local repository/branch need pushing and/or pulling, and if so, which? By "needs pulling" I mean that the remote contains commits I've not got, and by "needs pushing" I mean that I've got commits the remote hasn't got. (i.e. assume a linear workflow with nothing esoteric.)
If I could use a URL rather than a name that would be even better, but as far as I'm aware git is only able to describe remotes after calling git remote update.
Maybe even easier (depending on what you want to achieve):
$ git rev-list--count <branch-name>..<remote>/<remote-branch-name>Will output the number of commits the first branch is behind the remote branch
$ git rev-list--count <remote>/<remote-branch-name>..<branch-name>Will output the number of commits the first branch is ahead the remote branch
With this command you can list all sorts of things you might want to know. But you need to understand gitrevisions first.
$ git rev-list --count <some git revision specification>
Depending of what exactly you are going to do with this information the following might come in handy:
$ git branch -u <upstream># short for --set-upsteam-to=<upstream>
to set the upstream of the current branch
$ git status --porcelain=v2 --branch to get a very easily parsable output containing information about the upstream relation
The output would look for example like this:
# branch.oid <hash>
# branch.head example-branch
# branch.upstream origin/example-branch-developer-1
# branch.ab +0 -1
The last line being the most interesting for you with the following format:
# branch.ab +<ahead> -<behind>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With