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()
Answer from underrun on Stack Overflow
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › tutorial.html
GitPython Tutorial — GitPython 3.1.46 documentation
In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.
🌐
Readthedocs
gitpython.readthedocs.io › en › stable › quickstart.html
GitPython Quick Start Tutorial — GitPython 3.1.46 documentation
Designed for developers seeking a practical and interactive learning experience, this concise resource offers step-by-step code snippets to swiftly initialize/clone repositories, perform essential Git operations, and explore GitPython’s capabilities.
🌐
GitHub
github.com › gitpython-developers › GitPython
GitHub - gitpython-developers/GitPython: GitPython is a python library used to interact with Git repositories. · GitHub
If you want to do that and you want the versions in GitPython's git submodules to be used, then pass -e git/ext/gitdb and/or -e git/ext/gitdb/gitdb/ext/smmap to pip install. This can be done in any order, and in separate pip install commands or the same one, so long as -e appears before each path. For example...
Starred by 5.1K users
Forked by 968 users
Languages   Python
🌐
Full Stack Python
fullstackpython.com › blog › first-steps-gitpython.html
First Steps with GitPython - Full Stack Python
November 30, 2017 - The os module makes it easy to read environment variables, such as our GIT_REPO_PATH variable we set earlier. from git import Repo gives our application access to the GitPython library when we create the Repo object. COMMITS_TO_PRINT is a constant that limits the number of lines of output based on the amount of commits we want our script to print information on.
🌐
Grimoire
grimoire.carcano.ch › the grimoire of a modern linux professional › blog › dev-ops tools › scripting › git with python howto gitpython tutorial and pygit2 tutorial
Git With Python HowTo GitPython Tutorial And PyGit2 Tutorial
November 14, 2024 - My affectionate readers know I always show things in action: theory is important, but worth few without some good practical examples. So I wrote two sample scripts, one using gitpython and the other using pygit2: you can use them as a starting point to write your own, improving and adapting them as needed.
🌐
Readthedocs
gitpython.readthedocs.io › en › 0.3.2 › tutorial.html
GitPython Tutorial — GitPython 0.3.2 documentation
In the following brief example, you will learn about the very basics, assuming you operate on the Git-Python repository itself: >>> repo = Repo('path/to/git-python/repository') >>> sms = repo.submodules [git.Submodule(name=gitdb, path=lib/git/ext/gitdb, url=git://github.com/gitpython-developers/GitPython.git, branch=master)] >>> sm = sms[0] >>> sm.name 'gitdb' >>> sm.module() # The module is the actual repository referenced by the submodule <git.Repo "<prefix>/git-python/lib/git/ext/gitdb/.git"> >>> sm.module_exists() True >>> sm.abspath == sm.module().working_tree_dir # the submodule's absolu
🌐
Readthedocs
gitpython.readthedocs.io › en › 0.3 › tutorial.html
GitPython Tutorial — GitPython 0.3.4 documentation
In the following brief example, you will learn about the very basics, assuming you operate on the Git-Python repository itself: >>> repo = Repo('path/to/git-python/repository') >>> sms = repo.submodules [git.Submodule(name=gitdb, path=lib/git/ext/gitdb, url=git://github.com/gitpython-developers/GitPython.git, branch=master)] >>> sm = sms[0] >>> sm.name 'gitdb' >>> sm.module() # The module is the actual repository referenced by the submodule <git.Repo "<prefix>/git-python/lib/git/ext/gitdb/.git"> >>> sm.module_exists() True >>> sm.abspath == sm.module().working_tree_dir # the submodule's absolu
🌐
Medium
zchelseal.medium.com › automate-your-git-workflow-with-gitpython-3320e833c4e2
Automate Your Git Workflow with GitPython | by Chelsea Liu | Medium
April 13, 2021 - The full GitPython documentation lives here, in which the tutorial section contains extensive information with examples like how to create a repo from scratch, but I found it a little difficult to navigate for this particular use case.
Find elsewhere
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.

🌐
GitHub
github.com › gitpython-developers › GitPython › blob › master › doc › source › tutorial.rst
GitPython/doc/source/tutorial.rst at master · gitpython-developers/GitPython
In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.
Author   gitpython-developers
🌐
YouTube
youtube.com › mike møller nielsen
Gitpython Introduction Do Git Actions From Python - YouTube
Enjoy! :-)Thank you for commenting and asking questions.Discord server - Where we discuss programming languages and tech - Please use the right channel to yo...
Published   July 25, 2021
Views   8K
🌐
Blue Book
lyz-code.github.io › blue-book › coding › python › gitpython
GitPython - The Blue Book
pip install GitPython · from git import Repo repo = Repo.init("path/to/initialize") If you want to get the working directory of the Repo object use the working_dir attribute. from git import Repo repo = Repo("existing/git/repo/path") from git import Repo Repo.clone_from(git_url, repo_dir) Given a repo object: index = repo.index # add the changes index.add(["README.md"]) from git import Actor author = Actor("An author", "author@example.com") committer = Actor("A committer", "committer@example.com") # commit by commit message and author and committer index.commit("my commit message", author=author, committer=committer) When building fake data, creating commits in other points in time is useful.
🌐
Readthedocs
gitpython.readthedocs.io › en › 2.0.1 › tutorial.html
GitPython Tutorial — GitPython 2.0.1 documentation
In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.
🌐
Readthedocs
gitpython.readthedocs.io
GitPython Documentation — GitPython 3.1.46 documentation
GitPython Tutorial · Meet the Repo type · Examining References · Modifying References · Understanding Objects · The Commit object · The Tree object · The Index Object · Handling Remotes · Submodule Handling · Obtaining Diff Information · Switching Branches ·
🌐
Readthedocs
gitpython.readthedocs.io › en › 0.1.7 › tutorial.html
GitPython Tutorial — GitPython 0.1.7 documentation
GitPython provides object model access to your git repository. Once you have created a repository object, you can traverse it to find parent commit(s), trees, blobs, etc. The first step is to create a Repo object to represent your repository. >>> from git import * >>> repo = Repo("/Users/mtrier/Development/git-python") In the above example, the directory /Users/mtrier/Development/git-python is my working repository and contains the .git directory.
🌐
PDX
web.pdx.edu › ~gjay › teaching › mth271_2020 › html › 03_Working_with_git.html
Git Repo class in python
The object repo stores information about this folder, which you gave to the constructor in the string variable repodir, in a data member called working_dir. You can access any data members of an object in memory, and you do so just like you access a method, using a dot . followed by the member name. Here is an example:
🌐
Azzamsa
azzamsa.com › n › gitpython-intro
Getting Started with GitPython - azzamsa.com
December 14, 2019 - A gentle introduction with a brief code example to jump into GitPython.
🌐
AskPython
askpython.com › home › how to use gitpython to pull remote repository?
How To Use GitPython To Pull Remote Repository? - AskPython
June 30, 2023 - In my case, since my destination folder is in the same directory, I can use “arduino” as the path for the local directory. You can specify the relative path according to your destination folder, even if the folder isn’t present, GitPython will create the folder and clone the contents of the repository at the given destination.
🌐
DevDungeon
devdungeon.com › content › working-git-repositories-python
Working with Git Repositories in Python | DevDungeon
March 17, 2020 - The [GitPython](https://gitpython.readthedocs.io/) project allows you to work in Python with Git repositories. In this guide we'll look at some basic operations like: - Initializing a repo - Cloning a repo - Adding and committing - Pushing and pulling with remotes - Checking for changes - Getting a diff - Listing and switching branches For full documentation, see [https://gitpython.readthedocs.io/](https://gitpython.readthedocs.io/). The source code is available at [https://github.com/gitpython-developers/GitPython](https://github.com/gitpython-developers/GitPython).