GitPython
GitPython is a Python library used to interact with Git repositories. The current version is 3.1.46, released on March 28, 2026. It follows a regular release cadence, with updates addressing bug fixes, feature enhancements, and security patches.
Warnings
- breaking GitPython requires Python 3.7 or higher. Attempting to use it with an older version will result in an ImportError.
- gotcha Ensure that both 'gitdb' and 'smmap' are installed, as they are dependencies for GitPython. Missing these can lead to ImportError.
Install
-
pip install gitpython
Imports
- Repo
from git import Repo
Quickstart
import os
from git import Repo
# Set the repository path
repo_path = os.environ.get('REPO_PATH', '/path/to/your/repo')
# Initialize the repository
repo = Repo(repo_path)
# Access the latest commit
commit = repo.head.commit
# Print the commit message
print(f'Latest commit: {commit.message}')