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 / 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
gitCLI must be installed and available onPATH:pipdoes not bundle a Git client; it delegates cloning to the systemgitexecutable. Ifgitis missing,pipfails withERROR: 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.tomlorsetup.py(e.g.,0.1.0), runningpip install "git+...@branch"again printsRequirement already satisfiedand skips pulling new commits! Forcepipto re-clone and rebuild the latest commit on that branch with:(Omitpython -m pip install --force-reinstall --no-deps "git+[https://github.com/org/repo.git@feature-branch](https://github.com/org/repo.git@feature-branch)"--no-depsif the branch also updated its own dependencies.)
- If the branch already has the same version string in
- Pinning a specific commit SHA or tag instead of a moving branch for reproducible builds:
- Branches move over time; for production
requirements.txtfiles, replace_namewith an immutable commit hash (1b2c3d4e5f6...) or release tag (1.4.2) using the exact same@refsyntax.
- Branches move over time; for production
- 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 colongithub.com:org/repoused in standardgit clonemust be replaced with a slashgithub.com/org/repoingit+ssh://URLs!). - HTTPS Personal Access Token (PAT): Inject the token via environment variable (
"git+https://${GITHUB_TOKEN}@github.com/org/repo.git@branch") or configuregit config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"so secrets are never hardcoded inrequirements.txt.
- SSH deploy key: Use
- 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-namein editable mode.
- If you want to edit the branch locally and test changes live without reinstalling, pass
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. |
You must log in or register to comment.
