使用HTTPS和gitpython克隆私有仓库 [英] Cloning a private repo using HTTPS with gitpython

查看:313
本文介绍了使用HTTPS和gitpython克隆私有仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gitpython通过HTTPS克隆git存储库.如果项目是私人仓库,则将提示输入用户名和密码.如何与提示进行python交互以传递用户名和密码变量?

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)

运行此代码的输出:

$ python example.py
Username for 'https://github.com': example
Password for 'https://example@github.com': 

我知道可以在URL中包含用户名和密码:

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.

推荐答案

当使用github访问令牌而不是可能需要2FA的用户名和密码时,它对我有用:

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和gitpython克隆私有仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆