如何使用带有ssh密钥的GitPython



我想创建一个简短的git工作流,使用git的python库(GitPython(做一些事情。

如果有人成功地使用带有ssh密钥的GitPython来做一些基本的事情,比如git pull,请发帖。

遵循文档不起作用。


文档非常令人困惑,它直接跳到了我怀疑任何人、任何地方都会使用的最高级用例中,并且在使用类似于repo.function((和git.function((的调用之间来回切换是令人困惑的。


我只需要做一些简单的普通事情:拉、添加、提交、推送、合并等等。但我真的很难在文档中找到如何做最常见的事情——我希望这些项目位于任何文档的最前面。

此外,我不明白为什么有人会使用GitPython来自动化事情,然后在每次访问托管回购时暂停输入凭据。这些人是谁?

最常见的用例是执行pull/push等操作,即使用ssh键在远程操作的任何操作。

如果有人成功使用GitPythonssh密钥,请分享,我们将不胜感激。


我很乐意帮助分解当前文档,重新开始,首先使用最常见的用例,并提前添加ssh密钥使用功能,但我需要首先收集所有这些信息,并在参与其中之前构建一个简单功能库(我会的,这需要完成(。

GitPython只是Git可执行文件的包装器。在GitPython中使用ssh键与在Git本身中使用ssh密钥相同。

这是一个演示。我已经从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.'

正如您所看到的,它只是在调用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.

一旦我添加了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...

确保git fetch正常工作。那么GitPython应该可以工作了。

最新更新