Have you ever written a deployment script with subprocess.run(["git", "pull"]) and felt like there just must be a better way? There is! GitPython provides you with a real Python API for doing anything Git can do: clone, commit, create branches, diffs, read history, etc.
Installation
pip install gitpython
Enter fullscreen mode
Exit fullscreen mode
GitPython assumes you have git installed on your system. It uses shelling out to git behind the scenes, translating into Python objects as cleanly as possible.
Opening a Repository
from git import Repo , InvalidGitRepositoryError
# Open an existing repo repo = Repo ( " /path/to/your/project " )
# Open the repo at the current working directory repo = Repo ( " . " )
# Open from any subdirectory (search_parent_directories=True) repo = Repo ( " .…