ssh github 有效,但 git push 无效。

Bra*_*nor 4 git ssh github

我从未遇到过 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.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

配置

我的 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
Run Code Online (Sandbox Code Playgroud)

我的 ssh 配置包括

Host github
  HostName github.com
  User git
  IdentityFile "~/.ssh/github_rsa"
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 5

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

git remote set-url origin github:bradyt/basic-computing-notes.git
Run Code Online (Sandbox Code Playgroud)

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


更简单的是,musiKk在评论中建议,将 ssh 配置的条目更改为github.com.

Host github.com github
  HostName github.com
  User git
  IdentityFile "~/.ssh/github_rsa"
Run Code Online (Sandbox Code Playgroud)

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

正如raphinesse在评论中提到

如果您仍然想使用快捷方式githubHost关键字允许多种模式。
ssh_config手册页

如果提供了多个模式,它们应该用空格分隔。

  • 或者,也可以将配置的第一行设置为“Host github.com”。那么您就不需要“HostName”和“User”指令。 (2认同)