PyGithub is a library for interacting with the GitHub API; it will not let you clone your repository or otherwise interact with a local git repository.

GitPython is a Python interface to git and can be used to clone a remote repository. In fact, there's an example of exactly that at the beginning of the tutorial to which you have linked.

That said, it's not clear from your question exactly what you hope to accomplish -- learning the git commandline interface is an important skill if you're going to be working with GitHub (or git in general). While you can probably perform many of the same tasks with GitPython, you are arguably going to be spending much of your time duplicating the existing functionality of the commandline tools.

Answer from larsks on Stack Overflow
Discussions

dvc: consider switching from GitPython
GitPython causes constant headache and considerable time loss investigating and fixing issues on windows with file not closed and processes not terminated soon enough. We have several places in our code where we were forced to use retrie... More on github.com
๐ŸŒ github.com
31
July 2, 2019
Python Git Module experiences? - Stack Overflow
What are people's experiences with any of the Git modules for Python? (I know of GitPython, PyGit, and Dulwich - feel free to mention others if you know of them.) I am writing a program which wil... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Most suitable python library for Github API v3 - Stack Overflow
I am looking for a python library for the Github APIv3 suitable for me. I found one library (python-github3) mentioned in the GH API docs. After playing around with it in ipython for an hour or tw... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I write a test for code that creates a git branch and makes a pull request, without actually doing either?
Are you intending to test that your code does the right thing, or that your code causes a PR to show up on github? Because those aren't the same thing. You're using pygithub to interact with github and do things like open new PRs, and it's the job of pygithub to ensure that when you call the library it ends up making an actual PR on github. So you shouldn't test if a PR shows up on github, that's pygithub's problem, not yours. It's up to them to test that. All you should test in your own code is that you call the library in the correct way. Very commonly in unit tests you make a mock object which you can then check in the test. You mock the create-pr function in pygithub, do some test that should make that function get called in a certain way, then check that the mock object was actually called that way. This lets you test that what your code is actually responsible for (calling the library the right way) happens without running code you're not interested in testing (the code in the library itself). More on reddit.com
๐ŸŒ r/learnprogramming
4
0
July 24, 2020
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ pygithub vs github3py which one is a safer bet for the future?
r/Python on Reddit: PyGithub vs github3py which one is a safer bet for the future?
August 17, 2020 - I am wondering which python github api library would be a better future bet, one that is more likely to be actively maintained. So far I identifiedโ€ฆ
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ PyGithub
PyGithub ยท PyPI
PyGitHub is a Python library to access the GitHub REST API.
      ยป pip install PyGithub
    
Published ย  Mar 22, 2026
Version ย  2.9.0
๐ŸŒ
Wayfair-incubator
wayfair-incubator.github.io โ€บ pygitops
pygitops Documentation
Best of all, it works extremely well with GitPython as well as PyGithub!
๐ŸŒ
GitHub
github.com โ€บ treeverse โ€บ dvc โ€บ issues โ€บ 2215
dvc: consider switching from GitPython ยท Issue #2215 ยท treeverse/dvc
July 2, 2019 - GitPython causes constant headache and considerable time loss investigating and fixing issues on windows with file not closed and processes not terminated soon enough. We have several places in our code where we were forced to use retrie...
Author ย  Suor
๐ŸŒ
Medium
zchelseal.medium.com โ€บ automate-your-git-workflow-with-gitpython-3320e833c4e2
Automate Your Git Workflow with GitPython | by Chelsea Liu | Medium
April 13, 2021 - There are other ways to create ... use the PyGithub library to authenticate with a GitHub token and publish a pull request on behalf of whichever account this token belongs to. I did not experiment with this option as it would introduce more steps than simply clicking on a link. The full GitPython documentation ...
Find elsewhere
๐ŸŒ
Martin Heinz
martinheinz.dev โ€บ blog โ€บ 25
All the Things You Can Do With GitHub API and Python | Martin Heinz | Personal Website & Blog
June 15, 2020 - In both cases we start by taking GitHub token from environment variable. Next, in the example with using PyGitHub we use the token to create instance of GitHub class, which is then used to get repository and query its issues in open state.
๐ŸŒ
Medium
medium.com โ€บ @dhani281 โ€บ comparing-and-merging-git-branches-using-python-4e43fcfd3c5d
Comparing and Merging Git branches using Python | by Dhani Sebastian | Medium
May 27, 2023 - GitHub, one of the most popular version control platforms, provides powerful features for branch management. Comparing and merging branches are common tasks when incorporating changes into the main branch. In this tutorial, we will explore how to compare and merge two branches in a GitHub repository using the PyGithub library and Anaconda.
๐ŸŒ
Readthedocs
pygithub.readthedocs.io โ€บ en โ€บ latest โ€บ introduction.html
Introduction โ€” PyGithub 0.1.dev1+g7d1ba281e documentation
With it, you can manage your Github resources (repositories, user profiles, organizations, etc.) from Python scripts. Should you have any question, any remark, or if you find a bug, or if there is something you can do with the API but not with PyGithub, please open an issue.
๐ŸŒ
GitHub
github.com โ€บ PyGithub โ€บ PyGithub
GitHub - PyGithub/PyGithub: Typed interactions with the GitHub API v3 ยท GitHub
PyGitHub is a Python library to access the GitHub REST API.
Starred by 7.7K users
Forked by 1.9K users
Languages ย  Python 99.6% | Shell 0.4%
๐ŸŒ
Snyk
snyk.io โ€บ advisor โ€บ python packages โ€บ pygithub
PyGithub - Python Package Health Analysis | Snyk
August 21, 2021 - We found that PyGithub demonstrates a positive version release cadence with at least one new version released in the past 12 months. As a healthy sign for on-going project maintenance, we found that the GitHub repository had at least 1 pull request or issue interacted with by the community.
Top answer
1 of 11
136

While this question was asked a while ago and I don't know the state of the libraries at that point, it is worth mentioning for searchers that GitPython does a good job of abstracting the command line tools so that you don't need to use subprocess. There are some useful built in abstractions that you can use, but for everything else you can do things like:

import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()

Everything else in GitPython just makes it easier to navigate. I'm fairly well satisfied with this library and appreciate that it is a wrapper on the underlying git tools.

UPDATE: I've switched to using the sh module for not just git but most commandline utilities I need in python. To replicate the above I would do this instead:

import sh
git = sh.git.bake(_cwd='/home/me/repodir')
print git.status()
# checkout and track a remote branch
print git.checkout('-b', 'somebranch')
# add a file
print git.add('somefile')
# commit
print git.commit(m='my commit message')
# now we are one commit ahead
print git.status()
2 of 11
87

I thought I would answer my own question, since I'm taking a different path than suggested in the answers. Nonetheless, thanks to those who answered.

First, a brief synopsis of my experiences with GitPython, PyGit, and Dulwich:

  • GitPython: After downloading, I got this imported and the appropriate object initialized. However, trying to do what was suggested in the tutorial led to errors. Lacking more documentation, I turned elsewhere.
  • PyGit: This would not even import, and I could find no documentation.
  • Dulwich: Seems to be the most promising (at least for what I wanted and saw). I made some progress with it, more than with GitPython, since its egg comes with Python source. However, after a while, I decided it may just be easier to try what I did.

Also, StGit looks interesting, but I would need the functionality extracted into a separate module and do not want wait for that to happen right now.

In (much) less time than I spent trying to get the three modules above working, I managed to get git commands working via the subprocess module, e.g.

def gitAdd(fileName, repoDir):
    cmd = ['git', 'add', fileName]
    p = subprocess.Popen(cmd, cwd=repoDir)
    p.wait()

gitAdd('exampleFile.txt', '/usr/local/example_git_repo_dir')

This isn't fully incorporated into my program yet, but I'm not anticipating a problem, except maybe speed (since I'll be processing hundreds or even thousands of files at times).

Maybe I just didn't have the patience to get things going with Dulwich or GitPython. That said, I'm hopeful the modules will get more development and be more useful soon.

๐ŸŒ
Snyk
snyk.io โ€บ advisor โ€บ python packages โ€บ gitpython
GitPython - Python Package Health Analysis | Snyk
March 12, 2023 - We found that GitPython demonstrates a positive version release cadence with at least one new version released in the past 3 months. As a healthy sign for on-going project maintenance, we found that the GitHub repository had at least 1 pull ...
๐ŸŒ
Libhunt
python.libhunt.com โ€บ pygithub-alternatives
PyGitHub Alternatives - Python Third-party APIs | LibHunt
January 18, 2026 - PyGitHub is a Python library to access the GitHub API v3 and Github Enterprise API v3.
๐ŸŒ
Readthedocs
gitpython.readthedocs.io โ€บ en โ€บ stable โ€บ tutorial.html
GitPython Tutorial โ€” GitPython 3.1.46 documentation
Submodules can be conveniently handled using the methods provided by GitPython, and as an added benefit, GitPython provides functionality which behave smarter and less error prone than its original c-git implementation, that is GitPython tries hard to keep your repository consistent when updating submodules recursively or adjusting the existing configuration.