首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GitPython和SSH密钥?

GitPython和SSH密钥?
EN

Stack Overflow用户
提问于 2015-02-03 05:24:10
回答 8查看 29.2K关注 0票数 25

如何使用GitPython和特定的SSH键?

关于那个问题的文件不是很透彻。到目前为止,我唯一尝试过的就是Repo(path)

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2015-02-03 06:12:58

请注意,以下所有内容只适用于GitPython v0.3.6或更高版本。

您可以使用GIT_SSH环境变量为git提供一个可执行文件,git将代替它调用ssh。这样,每当git试图连接时,您都可以使用任何类型的ssh键。

这要么是使用上下文管理器的每个呼叫.

代码语言:javascript
复制
ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
with repo.git.custom_environment(GIT_SSH=ssh_executable):
    repo.remotes.origin.fetch()

..。或者更持久地使用存储库的set_environment(...)对象的Git方法:

代码语言:javascript
复制
old_env = repo.git.update_environment(GIT_SSH=ssh_executable)
# If needed, restore the old environment later
repo.git.update_environment(**old_env)

由于您可以设置任意数量的环境变量,所以可以使用一些将信息传递给ssh-script,以帮助它为您选择所需的ssh键。

关于这个特性的更多信息(在GitPython v0.3.6中是新的),您可以找到在相关问题上

票数 12
EN

Stack Overflow用户

发布于 2016-12-20 22:57:07

在gitpython==2.1.1上为我工作过

代码语言:javascript
复制
import os
from git import Repo
from git import Git

git_ssh_identity_file = os.path.expanduser('~/.ssh/id_rsa')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file

with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
     Repo.clone_from('git@....', '/path', branch='my-branch')
票数 15
EN

Stack Overflow用户

发布于 2019-11-26 09:08:49

我在GitPython==3.0.5上,下面的内容对我很有帮助。

代码语言:javascript
复制
from git import Repo
from git import Git    
git_ssh_identity_file = os.path.join(os.getcwd(),'ssh_key.key')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
Repo.clone_from(repo_url, os.path.join(os.getcwd(), repo_name),env=dict(GIT_SSH_COMMAND=git_ssh_cmd))

使用repo.git.custom_environment设置GIT_SSH_COMMAND不适用于clone_from函数。参考资料:https://github.com/gitpython-developers/GitPython/issues/339

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28291909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档