Search code examples
pythongitgithub-apipygithub

PyGitHub: Get Total number of commits for a repository


I am trying to get total number of Commits for each repository for repositories using Python GitHub.

Code:

 from github import Github
 git = Github("token")
 org = git.get_organization('organization')

 for repo in org.get_repos():
        repository_commit_date = repo.get_commit(sha='master')
        stats_ = repository_commit_date.stats
        print(stats_.total)

The code returns something else and it doesn't match the actual number of commits for the repositories. Can someone help me with this?

I want the output to look like:

Output:

 Repository Name: hello-world
 Number of commits: 62

Solution

  • After some googling I was able to get total number of commits for a GitHub repository.

    from github import Github
    g = Github("username","password")
    for repo in g.get_user().get_repos():
        print(repo.name, repo.get_commits().totalCount)
    

    For more information search here: https://github.com/PyGithub/PyGithub