pip install from git repo branch
Categories:
Installing Python Packages Directly from Git Repository Branches with pip

Learn how to leverage pip to install Python packages directly from specific branches, tags, or commits within a Git repository, covering various authentication methods and use cases.
When developing Python applications, you often encounter scenarios where a package isn't yet available on PyPI, or you need to use a specific version, a development branch, or even a particular commit from its Git repository. pip
, Python's package installer, provides robust support for installing packages directly from Git, offering flexibility that goes beyond standard PyPI installations. This article will guide you through the various methods to install packages from Git, including specifying branches, tags, and commits, and handling different authentication requirements.
Basic Installation from a Git Repository
The most straightforward way to install a package from a Git repository is to provide pip
with the repository's URL. By default, pip
will attempt to install the master
or main
branch. You can specify the protocol (e.g., git+https
, git+ssh
) depending on your repository's setup and authentication needs.
pip install git+https://github.com/username/repository.git
Installing from a public Git repository via HTTPS
Specifying Branches, Tags, or Commits
One of the most powerful features of pip
's Git integration is the ability to target specific versions of a package that aren't necessarily released. You can append @<ref>
to the repository URL, where <ref>
can be a branch name, a tag name, or a full commit hash. This is invaluable for testing new features, rolling back to previous versions, or using pre-release development versions.
flowchart TD A[Start] --> B{Specify Git URL?} B -- Yes --> C{Add @<ref>?} C -- Yes --> D[Install specific branch/tag/commit] C -- No --> E[Install default branch (master/main)] B -- No --> F[Install from PyPI] D --> G[End] E --> G
Decision flow for pip Git installation
# Install from a specific branch named 'develop'
pip install git+https://github.com/username/repository.git@develop
# Install from a specific tag, e.g., 'v1.2.3'
pip install git+https://github.com/username/repository.git@v1.2.3
# Install from a specific commit hash
pip install git+https://github.com/username/repository.git@a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
Examples of installing from branches, tags, and specific commits
Installing from Private Repositories with Authentication
Accessing private Git repositories requires authentication. pip
supports various methods, primarily through SSH or by embedding credentials directly into the URL for HTTPS. For security reasons, embedding credentials directly in the URL is generally discouraged, especially in shared environments or version control. SSH is the preferred method for private repositories.
# Using SSH (requires SSH key setup and agent running)
pip install git+ssh://git@github.com/username/private_repository.git@main
# Using HTTPS with embedded credentials (less secure, use with caution)
pip install git+https://your_token@github.com/username/private_repository.git@develop
# Using HTTPS with username and password (prompts for password if not in URL)
pip install git+https://username@github.com/username/private_repository.git@feature-branch
Authentication methods for private Git repositories
Editable Installations and Subdirectories
pip
also allows for "editable" installations, which means changes made to the source code in the cloned repository are immediately reflected in your Python environment without needing to reinstall. This is extremely useful for developing packages locally. Additionally, if the Python package is located in a subdirectory within the Git repository, you can specify its path.
# Editable installation from a branch
pip install -e git+https://github.com/username/repository.git@develop#egg=my_package
# Installing a package from a subdirectory within the repo
pip install git+https://github.com/username/repository.git@main#subdirectory=path/to/my_package
Editable installations and specifying subdirectories
#egg=my_package
part is crucial for editable installations, as it tells pip
the name of the package it's installing. The #subdirectory=
part specifies the relative path to the package's setup.py
or pyproject.toml
file within the repository.By mastering these pip
commands, you gain significant control over your Python project's dependencies, allowing you to integrate development versions, specific fixes, or unreleased features directly from their Git sources. This flexibility is a cornerstone of robust Python development workflows.