Check local Python status relative to remote with GitPython

How can I use GitPython to determine:

  • My local branch is ahead of the remote one (I can safely click)
  • My local branch is behind the remote control (I can pull it out safely)
  • Has my local branch deviated from the console?

To check if local and remote are the same, I do this:

def local_and_remote_are_at_same_commit(repo, remote): local_commit = repo.commit() remote_commit = remote.fetch()[0].commit return local_commit.hexsha == remote_commit.hexsha 
+7
source share
1 answer

See https://stackoverflow.com>

eg.

commits_behind = repo.iter_commits ('master..origin / master')

and

commits_ahead = repo.iter_commits ('origin / master..master')

Then you can use something like the following to go from an iterator to an account:

count = sum (1 for c in commits_ahead)

This is the last time verified using GitPython 1.0.2.

+7
source

All Articles