我用的是Mac雪豹,刚刚安装了git。

我只是试了

git clone git@thechaw.com:cakebook.git

但这给了我这个错误:

Initialized empty Git repository in `/Users/username/Documents/cakebook/.git/`
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

我错过了什么? 我也试过做ssh-keygen没有密码,但仍然错误。


当前回答

github帮助链接帮助我解决了这个问题。看起来ssh密钥没有添加到ssh-agent。这就是我最后做的事。

命令1:

确保ssh-agent已启用。在后台启动ssh-agent:

eval "$(ssh-agent -s)"

命令2:

将SSH密钥添加到SSH -agent:

ssh-add ~/.ssh/id_rsa

其他回答

如果用户未生成之前设置的ssh公私钥对

此信息在聊天上工作,但可以应用于所有其他支持SSH pubkey身份验证的git存储库。(参见[gitolite][1], gitlab或github。)

首先设置您自己的公钥/私钥对集。这 可以使用DSA或RSA,所以基本上你设置的任何密钥都可以工作。 在大多数系统上,您可以使用ssh-keygen。

First you'll want to cd into your .ssh directory. Open up the terminal and run: cd ~/.ssh && ssh-keygen Next you need to copy this to your clipboard. On OS X run: cat id_rsa.pub | pbcopy On Linux run: cat id_rsa.pub | xclip On Windows (via Cygwin/Git Bash) run: cat id_rsa.pub | clip On Windows (Powershell) run: Get-Content id_rsa.pub | Set-Clipboard (Thx to @orion elenzil) Add your key to your account via the website. Finally setup your .gitconfig. git config --global user.name "bob" git config --global user.email bob@... (don't forget to restart your command line to make sure the config is reloaded) That's it you should be good to clone and checkout.

更多信息请访问https://help.github.com/articles/generating-ssh-keys(感谢@Lee Whitney) [1]: https://github.com/sitaramc/gitolite

-

如果用户已经生成了之前设置的ssh公私钥对

检查哪个密钥已被授权在您的github或gitlab帐户设置 确定必须从本地计算机关联哪个相应的私钥

Eval $(ssh agent -s)

定义键的位置

ssh-add ~ / . ssh / id_rsa

Windows上的另一种可能性,在这些答案中都没有涉及,git或github文档中也没有涉及故障排除:

Git使用的openssh可执行文件可能与您认为的不同。

当我试图从github和ssh.dev.azure.com克隆或拉时,我收到了权限拒绝(公钥)错误,我遵循了所有的说明,并验证我的SSH密钥被正确设置(从SSH的立场)使用SSH -vT git@github.com和SSH -vT git@ssh.dev.azure.com。并且仍然得到这些错误:

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.

我最终发现问题在于Git for Windows和Windows都有自己的openssh版本。这里有文档:https://github.com/desktop/desktop/issues/5641

我依赖于Windows的ssh-agent服务来存储我的ssh密钥密码,所以git(使用它的单独版本的openssh)无法读取我的私钥。我认为这是一个错误,这个错误信息被使用-它是误导性的。

解决方案是:

git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

或者在~/.gitconfig中:

[core]
    sshCommand = 'C:\\Windows\\System32\\OpenSSH\\ssh.exe'

也许git很快就会修复这个问题,但这已经是我第二次在这个问题上浪费时间了。

使用来自Github的ssh链接,但确保不要附加ssh,只需使用git hub上的ssh选项卡来克隆你的repo。

当您正在访问SSH URL(读/写)而不是Git只读URL,但您没有对该repo的写访问权限时,可能会发生此错误。

有时你只是想克隆你自己的repo,例如部署到服务器上。在这种情况下,实际上只需要READ-ONLY访问。但由于这是你自己的回购,GitHub可能会显示SSH URL,如果这是你的偏好。在这种情况下,如果您的远程主机的公钥不在您的GitHub SSH密钥中,您的访问将被拒绝,这是预期会发生的。

一个等效的情况是,当您尝试克隆其他人的回购时,您没有SSH URL的写访问权。

总之,如果您的意图是只克隆一个repo,请使用HTTPS URL (https://github.com/{user_name}/{project_name}.git)而不是SSH URL (git@github.com:{user_name}/{project_name}.git),这样可以避免(不必要的)公钥验证。


更新:GitHub现在显示HTTPS作为默认协议,这一举动可能会减少SSH url的滥用。

您可以在终端中执行以下操作:

cd ~ / . ssh ssh-keygen -o -t rsa -C "your@email.com" 当提示输入密钥保存位置时,输入:cat id_rsa。Pub | pbcopy 输入并确认您的密码,然后生成新的密钥。 通过运行cat ~/.ssh/id_rsa.pub通知github密钥 复制输出,然后转到github创建一个新的SSH密钥。粘贴复制的内容到文本区域,给它一个合适的标题,然后按ADD键。

现在你可以轻松地进行操作了。