【问题标题】:ssh github works, but not git push.ssh github 有效,但 git push 无效。
【发布时间】:2022-02-17 19:42:48
【问题描述】:

我从来没有遇到过 ssh 工作和 git 不能以这种方式工作。不知道如何排除故障。

ssh 似乎可以工作(-T 阻止第一行):

iam@heeere:/e/.ssh$ ssh github
PTY allocation request failed on channel 0
Hi bradyt! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

git push 好像不行

iam@heeere:/e/basic-computing-notes$ git push
Permission denied (publickey).
fatal: Could not read from remote repository.

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

配置

我的 git 配置是

iam@heeere:/e/basic-computing-notes$ git config -l
user.email=algebrat@uw.edu
user.name=Brady Trainor
push.default=simple
alias.ac=!git add --all && git commit
alias.lol=log --oneline --graph --decorate --all
core.editor=vim
core.excludesfile=/e/configs/.gitignore_global
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:bradyt/basic-computing-notes.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

我的 ssh 配置包括

Host github
  HostName github.com
  User git
  IdentityFile "~/.ssh/github_rsa"

【问题讨论】:

标签: git github ssh


【解决方案1】:

由于您的 ssh 密钥没有默认名称(id_rsaid_rsa.pub),您需要使用您定义的 ssh 配置条目,以便您的 ssh url 引用正确的密钥:

git remote set-url origin github:bradyt/basic-computing-notes.git

这样,ssh 将寻找~/.ssh/github_rsa,而不是寻找~/.ssh/id_rsa


更简单,musiKk 建议 in the comments,将 ssh 配置的条目更改为 github.com

Host github.com github
  HostName github.com
  User git
  IdentityFile "~/.ssh/github_rsa"

为了确定,我保留了HostnameUser,但默认的 url 会起作用 (git@github.com:bradyt/basic-computing-notes.git)

正如raphinesse 提到in the comments

如果您仍想使用快捷方式 githubHost 关键字允许使用多种模式。
来自ssh_config man page

如果提供了多个pattern,则应以空格分隔。

【讨论】:

  • 或者,或者将配置的第一行设为Host github.com。那么你就不需要HostNameUser 指令了。
  • @musiKk 好点。我已将其包含在答案中以提高知名度。
  • @VonC 太棒了。我发现偏离“标准做事方式”通常会导致痛苦。您必须做出的假设越少越好。替代解决方案只需要修改一次而不是两次。
  • 如果您仍想使用快捷方式githubHost 关键字允许使用多种模式。来自ssh_config 手册页:如果提供了多个模式,则它们应该用空格分隔
  • @raphnesse 谢谢,好点子。我已将您的评论包含在答案中以提高知名度。
【解决方案2】:

我遇到了同样的问题,我发现我的 .gitconfig 中有一个条目将 ssh 替换为 https。

[url "https"]
    insteadOf = git

我可能在使用某些工具时不小心添加了此条目。删除后问题就解决了。

【讨论】:

    【解决方案3】:

    虽然这不是 OP 问题的答案,但我把它放在这里是为了其他可能像我一样最终来到这里的人:

    使用非标准SSH端口时,需要明确指定协议,即

    git remote set-url origin git+ssh://git@host:port/url.git

    而不是

    git remote set-url origin git@host:port/url.git

    【讨论】:

      【解决方案4】:

      我在使用“https”协议时遇到了同样的问题。

      Git push 不适用于“https”协议,但如果我手动将其更改为“git”,它可以工作。

      这不起作用:

      git push https://github.com/username/repo.git

      但将其更改为这样有效:

      git push git@github.com:username/repo.git

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        • 1970-01-01
        • 2012-01-31
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        相关资源
        最近更新 更多