Cloning a private repo using HTTPS with gitpython

和自甴很熟 提交于 2019-12-01 03:13:25

问题


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://example@github.com': 

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

HTTPS_REMOTE_URL = 'https://username:password@github.com/username/private-project'

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


回答1:


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>:x-oauth-basic@github.com/username/private-project'



来源:https://stackoverflow.com/questions/36358808/cloning-a-private-repo-using-https-with-gitpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!