简体   繁体   English

使用HTTPS和gitpython克隆私人仓库

[英]Cloning a private repo using HTTPS with gitpython

I am using gitpython to clone a git repository over HTTPS. 我正在使用gitpython通过HTTPS克隆git存储库。 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 ? 如何通过pythonically与提示进行交互以传递用户名和密码变量?

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: 我知道可以在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. 但是,如果这是一个私人回购,我无法提前知道。

当使用github访问令牌而不是用户名和密码时,它适用于我,其中可能需要2FA:

HTTPS_REMOTE_URL = 'https://<access_token>:x-oauth-basic@github.com/username/private-project'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM