当前位置:   article > 正文

git和python_GitPython和SSH密钥?

gitpython ssh

How can I use GitPython along with specific SSH Keys?

The documentation isn't very thorough on that subject. The only thing I've tried so far is Repo(path).

解决方案

Please note that all of the following will only work in GitPython v0.3.6 or newer.

You can use the GIT_SSH environment variable to provide an executable to git which will call ssh in its place. That way, you can use any kind of ssh key whenever git tries to connect.

This works either per call using a context manager ...

ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')

with repo.git.custom_environment(GIT_SSH=ssh_executable):

repo.remotes.origin.fetch()

... or more persistently using the set_environment(...) method of the Git object of your repository:

old_env = repo.git.update_environment(GIT_SSH=ssh_executable)

# If needed, restore the old environment later

repo.git.update_environment(**old_env)

As you can set any amount of environment variables, you can use some to pass information along to your ssh-script to help it pick the desired ssh key for you.

More information about the becoming of this feature (new in GitPython v0.3.6) you will find in the respective issue.

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/405895?site
推荐阅读
相关标签
  

闽ICP备14008679号