Git使用SSH公钥

生成SSH公钥

你可以按如下命令来生成 ssh key:

ssh-keygen -t rsa -C "itldg"

其中的 itldgssh key 的名称

Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxx itldg
The key's randomart image is:
+---[RSA 3072]----+
| ..+ .o.E        |
|o  o*. .         |
|o..++   . .      |
| +.+ o   +       |
|o   B   S        |
|+.oo =  .        |
|o++BBo o         |
|. **Boo          |
| +BB.            |
+----[SHA256]-----+

按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_rsa.pub 文件内容,获取到你的 public key

cat ~/.ssh/id_rsa.pub
ssh-rsa xxxxxxxxxxxxxxxxxxx itldg

添加SSH公钥

在git平台中添加SSH公钥(以gitee为例)

image

验证使用

首次使用需要确认并添加主机到本机SSH可信列表。

ssh -T git@gitee.com
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

输入yes,提示successfully 则证明添加成功。

Warning: Permanently added 'gitee.com,212.64.63.215' (ECDSA) to the list of known hosts.
Hi 老大哥! You've successfully authenticated, but GITEE.COM does not provide shell access.

image

拉取测试

image

复制SSH的拉取地址(公开的仓库,可用于测试)

git clone git@gitee.com:itldg/vscode-ldggist.git

image

Cloning into 'vscode-ldggist'...
remote: Enumerating objects: 133, done.
remote: Counting objects: 100% (133/133), done.
remote: Compressing objects: 100% (130/130), done.
Receiving objects:  90% (120/133), 1.64 MiB | 3.21 MiB/seused 0
Receiving objects: 100% (133/133), 1.85 MiB | 3.49 MiB/s, done.
Resolving deltas: 100% (67/67), done.

至此配置完成,测试成功

Tortoisegit(小乌龟)配置

上述操作完成后,使用小乌龟拉取项目会提示错误

错误弹窗

TortoiseGitPlink Fatal Error

No supported authentication methods available (server sent: publickey)

fatal: Could not read from remote repository.

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

默认设置

SSH 客户端从 C:\Program Files\TortoiseGit\bin\TortoiseGitPlink.exe

修改为 Git 的目录 C:\Program Files\Git\usr\bin\ssh.exe

克隆成功

posted @ 2023-01-19 15:07  IT老大哥  阅读(423)  评论(0编辑  收藏  举报