我用的是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没有密码,但仍然错误。


当前回答

我正与同样的问题作斗争,这就是我所做的,我能够克隆回购。我在Mac上遵循了这个步骤。

第一步:检查我们是否已经拥有公共SSH密钥。

打开终端。 输入ls -al ~/。查看是否存在现有的ssh密钥:

检查目录列表,查看是否已经拥有公共SSH密钥。默认public是以下d_dsa之一。酒吧,id_ecdsa。酒吧,id_ed25519。酒吧,id_rsa . pub。

如果你没有找到,那么转到步骤2,否则继续步骤3

步骤2:生成SSH公钥

Open Terminal. Enter the following command with a valid email address that you use for github ssh-keygen -t rsa -b 4096 -C "your_email@example.com" You will see the following in your terminal Generating public/private rsa key pair. When it prompts to"Enter a file in which to save the key," press Enter. This accepts the default file location. When it prompts to Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Just press enter again. At the prompt, "Type a secure passphrase. Enter passphrase (empty for no passphrase): [Type a passphrase]" press enter if you don't want to Enter same passphrase again: [Type passphrase again] press enter again

这将生成id_rsa.pub

步骤3:添加SSH密钥到SSH -agent

Interminal type eval "$(ssh-agent -s)" Add your SSH key to the ssh-agent. If you are using an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file. Enter this command $ ssh-add -K ~/.ssh/id_rsa Now copy the SSH key and also add it to you github account In terminal enter this command with your ssh file name pbcopy < ~/.ssh/id_rsa.pub This will copy the file to your clipboard Now open you github account Go to Settings > SSH and GPG keys > New SSH key Enter title and paste the key from clipboard and save it. Voila you're done.

其他回答

让我也分享一下我的经验,

我试图从Gerrit回购中克隆一些项目,我在帐户设置中获得了我的公钥。

在第一次尝试克隆git时,我得到了以下错误:

Unable to negotiate with XX.XX.XX.XX port XXX: no matching key exchange
method found. Their offer: diffie-hellman-group1-sha1

我发现我需要通过SSH选项-oKexAlgorithms=+diffie-hellman-group1-sha1以某种方式来git克隆。

希望GIT_SSH_COMMAND环境变量完成了这项工作:

export GIT_SSH_COMMAND="ssh -oKexAlgorithms=+diffie-hellman-group1-sha1"

但是git克隆仍然没有开始工作。现在它抛出(关于主题):

Permission denied (publickey).

我已经得到SSH密钥,不想重新生成它们。我检查了普通的SSH连接到主机,它是ok的:

****    Welcome to Gerrit Code Review    ****

  Hi XXXXX, you have successfully connected over SSH.

  Unfortunately, interactive shells are disabled.
  To clone a hosted Git repository, use:

  git clone ssh://xxxxx@xx.xx.xx:xxx/REPOSITORY_NAME.git

我有点困惑。我重新开始并通过-vvv选项打开SSH的调试。我看到了下面这些:

debug1: read_passphrase: can't open /dev/tty: No such device or address

可能,这是GIT_SSH_COMMAND env变量的开销-我的密钥是用passphrase保护的(我在检查登录到git repo主机时输入了它)。

所以,我决定去掉这个短语。一个简单的命令帮助了我:

ssh-keygen -p

然后,我输入了我的“旧密码”的密码短语,并在“新密码”上按了两次ENTER键,让它空着,即根本没有密码短语,并确认我的选择。

之后,我在本地磁盘上获得了新克隆的回购。

我遇到了同样的问题,因为我认为SSH和HTTPS之间的区别是

https://github.com/USERNAME/REPOSITORY.git

ssh: / / github.com/USERNAME/REPOSITORY.git

因此,我从HTTPS更改为SSH,只需将https://更改为SSH:// url的末尾没有任何变化。

但事实是:

https://github.com/USERNAME/REPOSITORY.git

git@github.com:USERNAME/REPOSITORY.git

这意味着我将ssh://github.com/USERNAME/REPOSITORY.git更改为git@github.com:USERNAME/REPOSITORY.git。

愚蠢的错误,但希望能帮助某人!

完成ssh-keygen部分后,您可以通过以下方式检查验证:

$ ssh -T git@github.com用于测试ssh连接; 确认是哪个用户执行了ssh-keygen命令。您生成的公钥后面跟着该用户。原因如下:

Git应该使用sudo命令吗? 你不应该在Git中使用sudo命令。如果您有非常好的理由必须使用sudo,那么请确保您在每个命令中都使用它(此时使用su作为根来获取shell可能更好)。如果您不使用sudo生成SSH密钥,然后尝试使用sudo git push之类的命令,那么您将不会使用您生成的相同密钥。

确认您对存储库将克隆到的目录具有写权限。如果没有,那就去做。更新用户组权限后,必须通过注销或su等方式刷新当前用户。

我也犯了同样的错误。

问题是,当将公钥复制到BitBucket(在我的情况下)时,在末尾复制/粘贴了一个不可见的换行符。

因此,在复制公钥时,首先将其复制到记事本,删除末尾的空行,复制并粘贴。

视觉指南(Windows)

1 / 2。Git批处理端

1.1. 打开git批处理(下载她)

1.2. 粘贴下面的文本(更改到您的GitHub帐户电子邮件)

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

1.3. 按Enter(接受默认文件位置)

1.4. 单击Enter两次(或设置SSH密钥passphrases - Gitbub passphrases docs)

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

1.5. 键生成:

您的身份信息已保存在/c/Users/user/.ssh/id_rsa…

1.6. 将SSH密钥复制到剪贴板。

$ clip < ~/.ssh/id_rsa.pub

2 / 2。Github网站用户端

在用户设置下

SSH和GPG密钥=>新的SSH密钥:

粘贴步骤1.6中的代码

:完成)


如果有人不想使用SSH,请使用HTTPS:

Github docs: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh