【发布时间】:2021-04-28 14:54:20
【问题描述】:
我已按照 this 指南生成密钥,并且已将其正确添加到我的 SSH 身份中
PS C:\Users\gianm\.ssh> ssh-add myGithub
Identity added: myGithub (*email_Address_I_Use_On_Github*)
我也给自己的Github账户添加了对应的公钥,看来我真的可以成功建立连接了。
PS C:\Users\gianm> ssh -T git@github.com
Hi *my_Username*! You've successfully authenticated, but GitHub does not provide shell access.
然后我将远程源添加到本地 git 存储库。
PS C:\Users\gianm\project> git remote -v
origin git@github.com:*my_Username*/*existing_Github_Repository*.git (fetch)
origin git@github.com:*my_Username*/*existing_Github_Repository*.git (push)
但如果我尝试执行 git push 之类的操作,就会发生这种情况。
PS C:\Users\gianm\project> git push -u origin master
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.
现在看起来很明显,我在 Windows 上,运行这个特定版本的 git:git version 2.31.1.windows.1。
【问题讨论】:
-
Git-for-Windows 有自己的 ssh,与 Windows 系统的 ssh 分开,原因我不明白(我不使用 Windows 所以这些原因可能存在,我只是不认识他们)。因此,Git ssh 很可能正在使用不同的方法来查找和呈现公钥。您可以将 Git 配置为使用特定的 ssh,使用
core.sshCommand(git config) 或GIT_SSH_COMMAND(环境)来调用特定的 ssh 版本,这可能会有所帮助。 -
据我所知,git-for-windows 有
git-bash.exe和bash.exe。他们有自己的主目录。git-bash在我的计算机上使用/c/Users/Username和bash使用/h/。ssh和git push可以从不同的.ssh文件夹中读取 ssh 密钥。.ssh通常在主目录下。 -
@torek 这成功了!我首先运行
which ssh来找到windows ssh 命令的目录,然后我将它作为GIT_SSH(不是GIT_SSH_COMMAND)添加到环境变量中。非常感谢,我不知道昨天花了多少时间寻找解决此问题的方法。 PS:git自带ssh命令的原因可能是老的windows版本没有捆绑openssh。 -
top level Git documentation 描述了两个环境变量,并表示
GIT_SSH_COMMAND优先。我不确定这里是否有关于使用哪一个的约定:如果您直接指向二进制文件,则任何一个都可以工作。另见GIT_SSH_VARIANT和ssh.variant的描述。 -
@torek 可能是它,我没有专门指ssh.exe,只是指它所在的目录。