274

I'm developing a django app and I'm using pip to manage my requirements. How can I do to install a specific git's commit?

In my case I need to install this commit: https://github.com/aladagemre/django-notification/commit/2927346f4c513a217ac8ad076e494dd1adbf70e1

kelwinfc
  • 3,101
  • 2
  • 14
  • 10

4 Answers4

460

You can specify commit hash, branch name, tag.

For the branch name and the tag, you can also install a compressed distribution. This is faster and more efficient, as it does not require cloning the entire repository. GitHub creates those bundles automatically.

hash:

$ pip install git+https://github.com/aladagemre/django-notification.git@2927346f4c513a217ac8ad076e494dd1adbf70e1

branch-name

With git

$ pip install git+https://github.com/aladagemre/django-notification.git@cool-feature-branch

or from source bundle

$ pip install https://github.com/aladagemre/django-notification/archive/cool-feature-branch.tar.gz

tag

with git

$ pip install git+https://github.com/aladagemre/django-notification.git@v2.1.0

or from source bundle

$ pip install https://github.com/aladagemre/django-notification/archive/v2.1.0.tar.gz

It is a not well-documented feature, but you can find more information at https://pip.pypa.io/en/latest/topics/vcs-support/

Hugo Lopes Tavares
  • 28,528
  • 5
  • 47
  • 45
  • 31
    It will add this fun message: `Could not find a tag or branch '2927346f4c513a217ac8ad076e494dd1adbf70e1', assuming commit.` – vlad-ardelean May 11 '15 at 09:05
  • 13
    @vlad-ardelean any idea on how to tell pip IT IS a commit? This is getting outputed on my deploy script and I don't want to suppress all stderr. – Leonardo Arroyo Sep 06 '16 at 21:42
  • If you do not specify anything, what is the behavior? – Scott Stafford Jun 12 '17 at 17:25
  • 1
    @ScottStafford You should not have a branch / tag which is called like a commit message. That would be ... strange. – Martin Thoma Aug 21 '17 at 11:33
  • 3
    In addition I would mention a HTTPS version of `git+` command: `pip install git+https://github.com/gpoore/codebraid@011464539bfb09b8611c8aef0d543532cea958bf`. It may be important for people behind corporate http proxies. – Grwlf Apr 24 '20 at 18:15
  • 1
    @LeonardoArroyo that warning is no longer printed as long as you use the full 40 character hash with recent versions of pip. See: https://github.com/pypa/pip/pull/4674 – benesch Jan 12 '21 at 15:48
  • 1
    The git+git://... version of this answer no longer works for GitHub effective today. Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ – Marco Poli Nov 02 '21 at 15:46
  • You might not need the 'v' at the beginning of the tag name… If you see the `Could not find a tag or branch` consider removing the 'v' and trying again. – Ted Stresen-Reuter May 13 '22 at 17:20
31

It's possible to automatically install a python package using the requirements.txt file on you project just by adding the following line:

package-name -e git+https://github.com/owner/repository.git@branch_or_commit#egg={package-name}

and run the command line:

$ pip install -r requirements.txt

mannysz
  • 951
  • 8
  • 19
  • 19
    For me (pip 9.0.1 in python3.5 virtualenv ) it didn't work : `pip install -r requirements.txt` raised 'Could not detect requirement name, please specify one with #egg='. But it worked with the format '-e git+https://github.com/owner/repository.git#egg=branch_or_commit' – Edouard Berthe Nov 17 '16 at 08:19
  • You need to use this format inside the requirements.txt file. Did you do that? – mannysz Nov 19 '16 at 17:10
  • 2
    I got it working but this is unclear. Need to have "package_name -e ..." and not just "-e ..." at the start of the line. – Udi Nov 06 '19 at 13:48
  • A working example, from a line in my requirements.txt: `python-openid -e git+https://github.com/openid/python-openid.git@d093a0919198eb53826ae5753e517af10ad95d5b#egg={python-openid}` – spodell Jun 17 '21 at 00:28
  • The `package -e url` syntax was not working for me. I'm guessing this was meant to signify an editable build but, well, it was not installing from Github when I tried this. Merely specifying the actual `git+https://github.com/...` URL like in the accepted answer worked for me with pip 19.2.3 on Python 3.8.2 – tripleee Apr 19 '22 at 18:44
25

An extra comment to @hugo-tavares's answer:

If it's a private GitHub repository, you'll need to use:

pip install git+ssh://git@github.com/....

In your case:

pip install git+ssh://git@github.com/aladagemre/django-notification.git@2927346f4c513a217ac8ad076e494dd1adbf70e1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
PGuiv
  • 537
  • 5
  • 11
3

If you want to create an egg package, you can still use the same @branch_or_commit appendage: pip install git+ssh://git@github.com/myrepo.git@mybranch#egg=myeggscript

Dannid
  • 1,507
  • 1
  • 20
  • 17