git: programmatically know by how much the branch is ahead/behind a remote branch

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.

Leave a Comment