Convert Figma logo to code with AI

gitpython-developers logoGitPython

GitPython is a python library used to interact with Git repositories.

4,564
899
4,564
157

Top Related Projects

1,606

Python bindings for libgit2

2,040

Pure-Python Git implementation

1,914

A simple version control system built on top of Git

10,240

Cross-platform lib for process and system monitoring in Python

Quick Overview

GitPython is a Python library used to interact with Git repositories. It provides a high-level interface to Git operations, allowing developers to manipulate Git repositories programmatically within their Python applications.

Pros

  • Easy to use and intuitive API for Git operations
  • Supports both high-level and low-level Git commands
  • Well-documented with extensive examples
  • Actively maintained and regularly updated

Cons

  • Can be slower than native Git commands for large repositories
  • Requires Git to be installed on the system
  • May have compatibility issues with certain Git versions
  • Learning curve for advanced usage and custom Git operations

Code Examples

  1. Cloning a repository:
from git import Repo

Repo.clone_from("https://github.com/example/repo.git", "/path/to/local/clone")
  1. Checking the status of a repository:
from git import Repo

repo = Repo("/path/to/repo")
print(repo.git.status())
  1. Committing changes:
from git import Repo

repo = Repo("/path/to/repo")
repo.git.add(".")
repo.index.commit("Commit message")
  1. Pushing changes to remote:
from git import Repo

repo = Repo("/path/to/repo")
origin = repo.remote("origin")
origin.push()

Getting Started

To get started with GitPython, follow these steps:

  1. Install GitPython using pip:

    pip install GitPython
    
  2. Import the necessary modules in your Python script:

    from git import Repo
    
  3. Create a Repo object to interact with a Git repository:

    repo = Repo("/path/to/your/repo")
    
  4. Use the Repo object to perform Git operations:

    # Example: Get the current branch name
    current_branch = repo.active_branch.name
    print(f"Current branch: {current_branch}")
    

Now you can use GitPython to interact with Git repositories in your Python projects.

Competitor Comparisons

1,606

Python bindings for libgit2

Pros of pygit2

  • Better performance due to C implementation
  • More comprehensive Git functionality
  • Thread-safe operations

Cons of pygit2

  • Steeper learning curve
  • More complex installation process
  • Less Pythonic API

Code Comparison

GitPython:

from git import Repo

repo = Repo("path/to/repo")
commit = repo.head.commit
print(commit.message)

pygit2:

import pygit2

repo = pygit2.Repository("path/to/repo")
commit = repo.head.peel(pygit2.Commit)
print(commit.message)

Both libraries allow for basic Git operations, but pygit2 offers more advanced features and better performance at the cost of a more complex API. GitPython provides a more Pythonic interface and is easier to install and use, making it suitable for simpler Git-related tasks. The choice between the two depends on the specific requirements of your project, such as performance needs, desired Git functionality, and development team expertise.

2,040

Pure-Python Git implementation

Pros of Dulwich

  • Pure Python implementation, no Git binary dependency
  • Supports lower-level Git operations and internals
  • Potentially faster for certain operations due to native implementation

Cons of Dulwich

  • Less high-level abstraction compared to GitPython
  • Smaller community and fewer resources available
  • May require more in-depth Git knowledge to use effectively

Code Comparison

GitPython:

from git import Repo

repo = Repo("path/to/repo")
commit = repo.head.commit
print(commit.message)

Dulwich:

from dulwich.repo import Repo

repo = Repo("path/to/repo")
commit = repo[repo.head()]
print(commit.message.decode('utf-8'))

Both libraries provide ways to interact with Git repositories, but GitPython offers a more Pythonic and high-level API, while Dulwich provides lower-level access to Git internals. GitPython is generally easier for beginners and common Git operations, while Dulwich offers more control and flexibility for advanced use cases. The choice between them depends on the specific requirements of your project and your familiarity with Git internals.

1,914

A simple version control system built on top of Git

Pros of Gitless

  • Simplified Git workflow, reducing complexity for users
  • Designed to be more intuitive and user-friendly
  • Focuses on common operations, making version control more accessible

Cons of Gitless

  • Less feature-rich compared to GitPython
  • Smaller community and fewer resources available
  • May not be suitable for advanced Git operations or complex workflows

Code Comparison

GitPython:

from git import Repo

repo = Repo("path/to/repo")
repo.git.add(".")
repo.index.commit("Commit message")
repo.remotes.origin.push()

Gitless:

import gitless.core as gl

gl.init()
gl.track(".")
gl.commit(m="Commit message")
gl.publish()

GitPython provides a more comprehensive API for interacting with Git repositories, while Gitless offers a simplified interface focused on common operations. GitPython is better suited for advanced Git operations and automation, whereas Gitless aims to make version control more accessible to users who find Git's complexity challenging.

10,240

Cross-platform lib for process and system monitoring in Python

Pros of psutil

  • Broader system monitoring capabilities, including CPU, memory, disks, network, and processes
  • Cross-platform support for Windows, Linux, macOS, FreeBSD, OpenBSD, NetBSD, and Sun Solaris
  • Actively maintained with frequent updates and bug fixes

Cons of psutil

  • Focused on system monitoring, not specific to version control operations
  • May require additional dependencies for certain functionalities on some platforms
  • Steeper learning curve for users primarily interested in Git operations

Code Comparison

psutil example:

import psutil

# Get CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
# Get memory usage
memory_info = psutil.virtual_memory()

GitPython example:

from git import Repo

# Clone a repository
Repo.clone_from("https://github.com/example/repo.git", "/path/to/local/repo")
# Get current branch
repo = Repo("/path/to/local/repo")
current_branch = repo.active_branch

While psutil focuses on system monitoring and resource management, GitPython specializes in Git operations and repository management. The choice between the two depends on the specific requirements of your project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Python package Documentation Status Packaging status

Gitoxide: A peek into the future…

I started working on GitPython in 2009, back in the days when Python was 'my thing' and I had great plans with it. Of course, back in the days, I didn't really know what I was doing and this shows in many places. Somewhat similar to Python this happens to be 'good enough', but at the same time is deeply flawed and broken beyond repair.

By now, GitPython is widely used and I am sure there is a good reason for that, it's something to be proud of and happy about. The community is maintaining the software and is keeping it relevant for which I am absolutely grateful. For the time to come I am happy to continue maintaining GitPython, remaining hopeful that one day it won't be needed anymore.

More than 15 years after my first meeting with 'git' I am still in excited about it, and am happy to finally have the tools and probably the skills to scratch that itch of mine: implement git in a way that makes tool creation a piece of cake for most.

If you like the idea and want to learn more, please head over to gitoxide, an implementation of 'git' in Rust.

(Please note that gitoxide is not currently available for use in Python, and that Rust is required.)

GitPython

GitPython is a python library used to interact with git repositories, high-level like git-porcelain, or low-level like git-plumbing.

It provides abstractions of git objects for easy access of repository data often backed by calling the git command-line program.

DEVELOPMENT STATUS

This project is in maintenance mode, which means that

  • …there will be no feature development, unless these are contributed
  • …there will be no bug fixes, unless they are relevant to the safety of users, or contributed
  • …issues will be responded to with waiting times of up to a month

The project is open to contributions of all kinds, as well as new maintainers.

REQUIREMENTS

GitPython needs the git executable to be installed on the system and available in your PATH for most operations. If it is not in your PATH, you can help GitPython find it by setting the GIT_PYTHON_GIT_EXECUTABLE=<path/to/git> environment variable.

  • Git (1.7.x or newer)
  • Python >= 3.7

The list of dependencies are listed in ./requirements.txt and ./test-requirements.txt. The installer takes care of installing them for you.

INSTALL

GitPython and its required package dependencies can be installed in any of the following ways, all of which should typically be done in a virtual environment.

From PyPI

To obtain and install a copy from PyPI, run:

pip install GitPython

(A distribution package can also be downloaded for manual installation at the PyPI page.)

From downloaded source code

If you have downloaded the source code, run this from inside the unpacked GitPython directory:

pip install .

By cloning the source code repository

To clone the the GitHub repository from source to work on the code, you can do it like so:

git clone https://github.com/gitpython-developers/GitPython
cd GitPython
./init-tests-after-clone.sh

On Windows, ./init-tests-after-clone.sh can be run in a Git Bash shell.

If you are cloning your own fork, then replace the above git clone command with one that gives the URL of your fork. Or use this gh command (assuming you have gh and your fork is called GitPython):

gh repo clone GitPython

Having cloned the repo, create and activate your virtual environment.

Then make an editable install:

pip install -e ".[test]"

In the less common case that you do not want to install test dependencies, pip install -e . can be used instead.

With editable dependencies (not preferred, and rarely needed)

In rare cases, you may want to work on GitPython and one or both of its gitdb and smmap dependencies at the same time, with changes in your local working copy of gitdb or smmap immediately reflected in the behavior of your local working copy of GitPython. This can be done by making editable installations of those dependencies in the same virtual environment where you install GitPython.

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, you can install GitPython, gitdb, and smmap editably in the currently active virtual environment this way:

pip install -e ".[test]" -e git/ext/gitdb -e git/ext/gitdb/gitdb/ext/smmap

The submodules must have been cloned for that to work, but that will already be the case if you have run ./init-tests-after-clone.sh. You can use pip list to check which packages are installed editably and which are installed normally.

To reiterate, this approach should only rarely be used. For most development it is preferable to allow the gitdb and smmap dependencices to be retrieved automatically from PyPI in their latest stable packaged versions.

Limitations

Leakage of System Resources

GitPython is not suited for long-running processes (like daemons) as it tends to leak system resources. It was written in a time where destructors (as implemented in the __del__ method) still ran deterministically.

In case you still want to use it in such a context, you will want to search the codebase for __del__ implementations and call these yourself when you see fit.

Another way assure proper cleanup of resources is to factor out GitPython into a separate process which can be dropped periodically.

Windows support

See Issue #525.

RUNNING TESTS

Important: Right after cloning this repository, please be sure to have executed the ./init-tests-after-clone.sh script in the repository root. Otherwise you will encounter test failures.

Install test dependencies

Ensure testing libraries are installed. This is taken care of already if you installed with:

pip install -e ".[test]"

If you had installed with a command like pip install -e . instead, you can still run the above command to add the testing dependencies.

Test commands

To test, run:

pytest

To lint, and apply some linting fixes as well as automatic code formatting, run:

pre-commit run --all-files

This includes the linting and autoformatting done by Ruff, as well as some other checks.

To typecheck, run:

mypy

CI (and tox)

Style and formatting checks, and running tests on all the different supported Python versions, will be performed:

  • Upon submitting a pull request.
  • On each push, if you have a fork with GitHub Actions enabled.
  • Locally, if you run tox (this skips any Python versions you don't have installed).

Configuration files

Specific tools are all configured in the ./pyproject.toml file:

  • pytest (test runner)
  • coverage.py (code coverage)
  • ruff (linter and formatter)
  • mypy (type checker)

Orchestration tools:

  • Configuration for pre-commit is in the ./.pre-commit-config.yaml file.
  • Configuration for tox is in ./tox.ini.
  • Configuration for GitHub Actions (CI) is in files inside ./.github/workflows/.

Contributions

Please have a look at the contributions file.

INFRASTRUCTURE

  • User Documentation
  • Questions and Answers
  • Please post on Stack Overflow and use the gitpython tag
  • Issue Tracker
    • Post reproducible bugs and feature requests as a new issue. Please be sure to provide the following information if posting bugs:
      • GitPython version (e.g. import git; git.__version__)
      • Python version (e.g. python --version)
      • The encountered stack-trace, if applicable
      • Enough information to allow reproducing the issue

How to make a new release

  1. Update/verify the version in the VERSION file.
  2. Update/verify that the doc/source/changes.rst changelog file was updated. It should include a link to the forthcoming release page: https://github.com/gitpython-developers/GitPython/releases/tag/<version>
  3. Commit everything.
  4. Run git tag -s <version> to tag the version in Git.
  5. Optionally create and activate a virtual environment. (Then the next step can install build and twine.)
  6. Run make release.
  7. Go to GitHub Releases and publish a new one with the recently pushed tag. Generate the changelog.

Projects using GitPython

LICENSE

3-Clause BSD License, also known as the New BSD License. See the LICENSE file.

One file exclusively used for fuzz testing is subject to a separate license, detailed here. This file is not included in the wheel or sdist packages published by the maintainers of GitPython.