Search code examples
pythongitgitpython

How to get count of unpublished commit with GitPython?


With git status I can get information about count of unpublished commits:

» git status             
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

I want to get unpublished commits (or count) with GitPython. I docs I found repo.git.status(), but this is not what I want.


Solution

  • The command you are looking for is:

    repo.iter_commits('BRANCH@{u}..BRANCH')
    

    or if you want this as a list:

    list(repo.iter_commits('BRANCH@{u}..BRANCH'))
    

    The BRANCH@{u} syntax refers to the upstream branch of BRANCH.