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/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:
origin/branch
but need not be.First use git remote update , to bring your remote refs up to date. Then you can do one of several things, such as: git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.
You can do this with a combination of git merge-base and git rev-parse . If git merge-base <branch> <remote branch> returns the same as git rev-parse <remote branch> , then your local branch is ahead. If it returns the same as git rev-parse <branch> , then your local branch is behind.
To check if you're up-to-date with GitHub run git fetch origin before git status and you'll know you're up-to-date.
To see all local and remote branches, run this command: git branch -a.
git rev-list origin..HEAD
will show the commits that are in your current branch, but not origin -- i.e., whether you're ahead of origin and by which commits.
git rev-list HEAD..origin
will show the opposite.
If both commands show commits, then you have diverged branches.
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