【问题标题】:Git pull using GitPython + SSH keys doesn't work使用 GitPython + SSH 密钥的 Git pull 不起作用
【发布时间】:2017-05-14 12:59:54
【问题描述】:

我正在尝试使用 GitPython 从我的 Github 帐户中提取一个 repo。这是在

之后

(1) 我已经从命令行执行了一个 git clone。

(2) 使用

生成新的 SSH 密钥
ssh-keygen -t rsa -b 4096

(3) 在 Github 中将上述 #2 中的 .pub 文件的内容设置为新的 SSH 密钥。

我仍然被提示输入我的 Github 用户名和密码。这是为什么呢?

这是我的 .git 文件夹中的配置。注意 url 中的 http:// 而不是 https://

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://github.com/githubuser/myrepo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

这是我的代码 sn-p

import git, os

DIR_NAME = 'path/to/dir/with/gitfolder'
git_ssh_identity_file = os.path.expanduser('/path/to/rsa/key')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file

with git.Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):

    repo = git.Repo.init(DIR_NAME)
    origin = repo.remote()
    refs = origin.refs

    for ref in refs:
        if ref.remote_head == "master":
            origin.pull(ref.remote_head)

【问题讨论】:

  • 你修改了私钥吗?

标签: python git github ssh gitpython


【解决方案1】:

注意 url 中的 http:// 而不是 https://4

只要您使用的是 http(s) url,ssh 就根本不会用于身份验证。

你需要一个 ssh 网址:

git@github.com:USERNAME/OTHERREPOSITORY.git

http url 仍然依赖于用户名/密码身份验证方案。
只有 ssh url 会使用您的公钥/私钥。

【讨论】:

  • 更改为 ssh url 有效,但现在我似乎收到此错误:cmdline: git pull -v origin master stderr: 'fatal: could not read from remote repository。请确保您拥有正确的访问权限并且存储库存在。我可以通过依次运行这 2 个来解决这个问题 - (1)ssh-agent -s (2)ssh-add ~/.ssh/id_rsa。然而,这似乎并没有持续存在,例如几分钟后(或新会话),它再次抱怨访问权限,直到我运行相同的 2 个命令。
  • @DancingJohn 您的私有 ssk 密钥是否受密码保护?你真的不需要密码:你可以在没有密码的情况下生成一个。
  • @DancingJohn 但是你需要有一个受密码保护的私钥,然后help.github.com/articles/… 是参考。确保您的代理自动启动,如help.github.com/articles/working-with-ssh-key-passphrases 中所述
  • 没有密码。虽然我的实际文件名是 github_id_rsa 而不是 id_rsa。希望这不是问题。
  • @DancingJohn 那你就不需要 ssh_agent。但是如果您的 ssh 密钥未命名为 id_rsa,您将需要 .ssh/config 文件:例如参见 stackoverflow.com/a/22769324/6309
猜你喜欢
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2023-01-05
  • 1970-01-01
  • 2021-01-30
相关资源
最近更新 更多