Skip to content
Advertisement

Check status of local Python relative to remote with GitPython

How can I use GitPython to determine whether:

  • My local branch is ahead of the remote (I can safely push)
  • My local branch is behind the remote (I can safely pull)
  • My local branch has diverged from the remote?

To check if the local and remote are the same, I’m doing this:

JavaScript

Advertisement

Answer

See https://stackoverflow.com/a/15862203/197789

E.g.

JavaScript

and

JavaScript

Then you can use something like the following to go from iterator to a count:

JavaScript

(You may want to fetch from the remotes before running iter_commits, eg: repo.remotes.origin.fetch())

This was last checked with GitPython 1.0.2.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement