简体   繁体   English

如何使用带有 ssh 密钥的 GitPython?

[英]How do I use GitPython with a ssh key?

I want to create a short git workflow to do a few things using a python library for git (GitPython).我想创建一个简短的 git 工作流,使用 git (GitPython) 的 python 库来做一些事情。

If anyone has successfully used GitPython with ssh keys to do basic things like a git pull, please post.如果有人成功地使用带有 ssh 键的 GitPython 来执行 git 拉动等基本操作,请发布。

Following the documentation does not work.遵循文档不起作用。


The documentation is very confusing, it jumps right into the most advanced use cases that I doubt anyone, anywhere would use, and going back and forth between using a call like: repo.function() and git.function() is confusing.该文档非常令人困惑,它直接跳到了我怀疑任何人、任何地方都会使用的最高级用例,并且在使用诸如repo.function()git.function() 之类的调用之间来回切换令人困惑。


I only need to do the simple usual things: pull, add, commit, push, merge etc. but am really struggling to dig through the documentation to find how to do the most common things - items that I would expect to be at the very front of any documentation.我只需要做一些简单的日常事情:拉取、添加、提交、推送、合并等,但我真的很难通过文档来找到如何做最常见的事情——我希望在非常任何文档的前面。

In addition, I do not understand why anyone would use GitPython to automate things and then pause to enter their credentials each time they hit a hosted repo.此外,我不明白为什么有人会使用GitPython来实现自动化,然后在每次访问托管存储库时暂停输入他们的凭据。 Who are these people?这些人是谁?

The most common use case would be to do a pull/push etc., anything that operates on the remote with a ssh key .最常见的用例是使用ssh 键在遥控器上进行拉/推等操作。

If anyone has successfully used GitPython with a ssh key , please share, it would be greatly appreciated.如果有人成功使用带有ssh密钥的 GitPython,请分享,将不胜感激。


I would be very glad to help shred the current documentation, start over, with the most common use cases first, and add ssh key use functionality up front, but I need to gather all of this information first and build a library of simple functionality before getting involved there (and I will - it needs to be done).我很乐意帮助粉碎当前文档,重新开始,首先使用最常见的用例,并预先添加 ssh 关键使用功能,但我需要先收集所有这些信息并构建一个简单功能库参与其中(我会 - 它需要完成)。

GitPython is just a wrapper around the Git executable. GitPython 只是 Git 可执行文件的包装器。 Using an ssh key with GitPython is the same as using one with Git itself .使用带有 GitPython 的 ssh 密钥与使用带有 Git 本身的密钥相同

Here is a demonstration.这是一个演示。 I have removed my ssh key from my ssh agent.我已从 ssh 代理中删除了我的 ssh 密钥。

>>> from git import Repo
>>> repo = Repo("/Users/schwern/devel/ruby")
>>> origin = repo.remote("origin")
>>> for fetch_info in origin.fetch():
...     print("Updated %s to %s" % (fetch_info.ref, fetch_info.commit))
... ^D
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/remote.py", line 797, in fetch
    res = self._get_fetch_info_from_stderr(proc, progress)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/remote.py", line 676, in _get_fetch_info_from_stderr
    proc.wait(stderr=stderr_text)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/git/cmd.py", line 408, in wait
    raise GitCommandError(self.args, status, errstr)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git fetch -v origin
  stderr: 'fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

As you can see, it's just calling git fetch -v origin .如您所见,它只是调用git fetch -v origin I get the same error if I run git fetch -v origin .如果我运行git fetch -v origin ,我会得到同样的错误。

$ git fetch -v origin
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Once I've added my ssh key, origin.fetch() succeeds.一旦我添加了我的 ssh 密钥, origin.fetch()成功。

>>> for fetch_info in origin.fetch():
...     print("Updated %s to %s" % (fetch_info.ref, fetch_info.commit))
... ^D
Updated origin/master to 7a3322a0fd660d676f1918bd7c4a37676b44e1c2
...etc...

Make sure git fetch works.确保git fetch工作正常。 Then GitPython should work.然后 GitPython 应该可以工作。

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

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