I'm developing a git plug-in, and I need to know when a local repo is changed (can commit changes), ahead (can push to remote) or behind (can pull from remote) using the command line.
This is what I am doing so far:
Can commit?
If git diff-index --name-only --ignore-submodules HEAD --
returns something,
then yes, there are changes to commit.
Can push?
If git status -sb
contains the word ahead in it's output, then yes, there
are commits to push.
Can pull?
Nothing implemented yet.
The can commit? part seems to work properly. Can push? only works for the master branch, and this is a huge problem.
How can I safely check if, on every branch, a git repo has changes to commit, commits to push, or needs a git pull
?
For future reference. As of Git v2.17.0
git status -sb
contains the word behind . So that can be used directly to check for pulls.
Note: Remember to run git fetch
before running git status -sb
From this answer.
git fetch
.behind_count = $(git rev-list --count HEAD..@{u})
.ahead_count = $(git rev-list --count @{u}..HEAD)
. (It assumes that where you fetch from is where you push to, see push.default
configuration option).behind_count
and ahead_count
are 0, then current branch is up to date.behind_count
is 0 and ahead_count
is greater than 0, then current branch is ahead.behind_count
is greater than 0 and ahead_count
is 0, then current branch is behind.behind_count
and ahead_count
are greater than 0, then current branch is diverged.Explanation:
git rev-list
list all commits of giving commits range. --count
option output how many commits would have been listed, and suppress all other output.HEAD
names current branch.@{u}
refers to the local upstream of current branch (configured with branch.<name>.remote
and branch.<name>.merge
). There is also @{push}
, it is usually points to the same as @{u}
.<rev1>..<rev2>
specifies commits range that include commits that are reachable from but exclude those that are reachable from . When either or is omitted, it defaults to HEAD.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. If merge-base
returns a different answer than either rev-parse
, then the branches have diverged and you'll need to do a merge.
It would be best to do a git fetch
before checking the branches, though, otherwise your determination of whether or not you need to pull will be out of date. You'll also want to verify that each branch you check has a remote tracking branch. You can use git for-each-ref --format='%(upstream:short)' refs/heads/<branch>
to do that. That command will return the remote tracking branch of <branch>
or the empty string if it doesn't have one. Somewhere on SO there's a different version which will return an error if the branch doesn't haven't a remote tracking branch, which may be more useful for your purpose.
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