簡體   English   中英

如何使用帶有 ssh 密鑰的 GitPython?

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

我想創建一個簡短的 git 工作流,使用 git (GitPython) 的 python 庫來做一些事情。

如果有人成功地使用帶有 ssh 鍵的 GitPython 來執行 git 拉動等基本操作,請發布。

遵循文檔不起作用。


該文檔非常令人困惑,它直接跳到了我懷疑任何人、任何地方都會使用的最高級用例,並且在使用諸如repo.function()git.function() 之類的調用之間來回切換令人困惑。


我只需要做一些簡單的日常事情:拉取、添加、提交、推送、合並等,但我真的很難通過文檔來找到如何做最常見的事情——我希望在非常任何文檔的前面。

此外,我不明白為什么有人會使用GitPython來實現自動化,然后在每次訪問托管存儲庫時暫停輸入他們的憑據。 這些人是誰?

最常見的用例是使用ssh 鍵在遙控器上進行拉/推等操作。

如果有人成功使用帶有ssh密鑰的 GitPython,請分享,將不勝感激。


我很樂意幫助粉碎當前文檔,重新開始,首先使用最常見的用例,並預先添加 ssh 關鍵使用功能,但我需要先收集所有這些信息並構建一個簡單功能庫參與其中(我會 - 它需要完成)。

GitPython 只是 Git 可執行文件的包裝器。 使用帶有 GitPython 的 ssh 密鑰與使用帶有 Git 本身的密鑰相同

這是一個演示。 我已從 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 應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM