GitPython 和 SSH 密钥?

新手上路,请多包涵

如何将 GitPython 与特定的 SSH 密钥一起使用?

该文档在该主题上不是很详尽。到目前为止我唯一尝试过的是 Repo(path)

原文由 andreihondrari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 789
2 个回答

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

您可以使用 GIT_SSH 环境变量为 git 提供一个可执行文件,它将调用 ssh 代替它。这样,只要 git 尝试连接,您就可以使用任何类型的 ssh 密钥。

这适用于每次调用使用 上下文管理器……

 ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
with repo.git.custom_environment(GIT_SSH=ssh_executable):
    repo.remotes.origin.fetch()

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

 old_env = repo.git.update_environment(GIT_SSH=ssh_executable)
# If needed, restore the old environment later
repo.git.update_environment(**old_env)

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

有关此功能的更多信息(GitPython v0.3.6 中的新功能),您将 在相应的问题中 找到。

原文由 Byron 发布,翻译遵循 CC BY-SA 3.0 许可协议

以下为我在 gitpython==2.1.1 上工作

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')

原文由 Vijay Katam 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题