Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning a private repo using HTTPS with gitpython

I am using gitpython to clone a git repository over HTTPS. If the project is a private repo, it will prompt for username and password. How do I interact with the prompt pythonically to pass username and password variables ?

from git import Repo

HTTPS_REMOTE_URL = 'https://github.com/username/private-project'
DEST_NAME = 'https-cloned-private-project'
cloned_repo = Repo.clone_from(HTTPS_REMOTE_URL, DEST_NAME)

Output of running this code:

$ python example.py
Username for 'https://github.com': example
Password for 'https://[email protected]': 

I know it's possible to include the username and password in the URL:

HTTPS_REMOTE_URL = 'https://username:[email protected]/username/private-project'

However, I have no way of knowing ahead of time if this is a private repo.

like image 673
Python Novice Avatar asked Apr 01 '16 14:04

Python Novice


People also ask

Can you clone private repo?

Yes! We can do that following these steps: Create a new Blank Project instead of Clone. Click the 'Configure Git' button and use the existing private repo for the git remote.


1 Answers

it works for me when using github access token instead of username and password where 2FA may be required:

HTTPS_REMOTE_URL = 'https://<access_token>:[email protected]/username/private-project'

like image 197
shawnzhu Avatar answered Sep 19 '22 13:09

shawnzhu