How do you install a Python package from a specific Git branch using pip? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you install a Python package from a specific Git branch using pip?

message-square
0

Pass the VCS URL prefixed with git+ and suffixed with @<branch_name> to python -m pip install—for example, python -m pip install "git+https://github.com/org/repo.git@feature-branch" (or append #egg=package_name / #subdirectory=subpath when installing from a monorepo)—always quoting the URL in your shell so @ or # characters are not interpreted as shell comments or special tokens.

Basic Example

# 1. Recommended: Install a specific branch over HTTPS (always quote the URL):
python -m pip install "git+[https://github.com/psf/requests.git@main](https://github.com/psf/requests.git@main)"

# 2. Install a branch from a private repository over SSH:
python -m pip install "git+ssh://git@github.com/my-org/private-repo.git@develop"

# 3. Install a package located inside a subdirectory of a Git branch (monorepo):
python -m pip install "git+[https://github.com/my-org/monorepo.git@release-v2#subdirectory=packages/core-lib](https://github.com/my-org/monorepo.git@release-v2#subdirectory=packages/core-lib)"

# 4. Syntax inside `requirements.txt` (PEP 508 direct reference format):
# my-pkg @ git+[https://github.com/my-org/repo.git@feature-branch](https://github.com/my-org/repo.git@feature-branch)

Key Details & Edge Cases

  • git CLI must be installed and available on PATH:
    • pip does not bundle a Git client; it delegates cloning to the system git executable. If git is missing, pip fails with ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?.
  • Re-installing when a branch receives new commits (--force-reinstall / --upgrade):
    • If the branch already has the same version string in pyproject.toml or setup.py (e.g., 0.1.0), running pip install "git+...@branch" again prints Requirement already satisfied and skips pulling new commits! Force pip to re-clone and rebuild the latest commit on that branch with:
      python -m pip install --force-reinstall --no-deps "git+[https://github.com/org/repo.git@feature-branch](https://github.com/org/repo.git@feature-branch)"
      
      (Omit --no-deps if the branch also updated its own dependencies.)
  • Pinning a specific commit SHA or tag instead of a moving branch for reproducible builds:
    • Branches move over time; for production requirements.txt files, replace @branch_name with an immutable commit hash (@a1b2c3d4e5f6...) or release tag (@v1.4.2) using the exact same @ref syntax.
  • Authenticating private Git repositories in CI/CD without interactive prompts:
    • SSH deploy key: Use "git+ssh://git@github.com/org/repo.git@branch" (note the colon github.com:org/repo used in standard git clone must be replaced with a slash github.com/org/repo in git+ssh:// URLs!).
    • HTTPS Personal Access Token (PAT): Inject the token via environment variable ("git+https://${GITHUB_TOKEN}@github.com/org/repo.git@branch") or configure git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/" so secrets are never hardcoded in requirements.txt.
  • Editable source checkout for active local development (-e):
    • If you want to edit the branch locally and test changes live without reinstalling, pass -e (python -m pip install -e "git+https://github.com/org/repo.git@branch#egg=pkg_name"), which clones the repo into <venv>/src/pkg-name in editable mode.

Comparison of Git Reference Specifiers in pip install

URL / Specifier Syntax Target Resolved Re-running pip install Pulls New Commits? Best Used When
"git+https://.../repo.git@branch-name" (Recommended) Latest commit on branch-name No (unless version bumped or --force-reinstall passed) Testing an unreleased feature branch, PR branch, or develop/main branch.
"git+https://.../repo.git@v1.2.0" Exact Git tag v1.2.0 N/A (immutable tag) Installing a tagged release not yet published to PyPI.
"git+https://.../repo.git@8f3a1c9..." Exact 7–40 char commit SHA N/A (immutable commit) Production requirements.txt / lockfiles requiring deterministic, reproducible builds.
"git+https://.../repo.git@branch#subdirectory=pkg" Subfolder pkg/ on branch No (unless --force-reinstall passed) Installing a Python package housed inside a multi-package monorepo.
"git+ssh://git@github.com/org/repo.git@branch" Private repo branch via SSH No (unless --force-reinstall passed) Private repositories authenticated via local SSH keys or CI deploy keys.