windows下 git配置github和gitee SSH-Key密钥方便同时将本地分支推送到远程仓库

发布于 2021-06-13  1606 次阅读


1先配置giteee的SSH-Key

生成一个gitee用的SSH-Key密钥:输入命令:(gitee_id_rsa是生成公钥的名)

$ ssh-keygen -t rsa -C '你的邮箱' -f ~/.ssh/gitee_id_rsa

点击三次回车,这里什么也不用填。

2.配置github的SSH-Key

生成一个github用的SSH-Key密钥:输入命令:(github_id_rsa是生成公钥的名)

$ ssh-keygen -t rsa -b 4096 -C "你的邮箱" -f ~/.ssh/github_id_rsa

点击三次回车,这里什么也不用填。

2.1 将SSH密钥添加到 ssh-agent,输入命令

$ eval $(ssh-agent -s)

返回类似这样的东西

> Agent pid 595

2.2将ssh密钥添加到github

$ ssh-add ~/.ssh/github_id_rsa

3:在C:\Users\你的用户名.ssh目录下可以看见升级的密钥文件,并新建一个config文件,添加如下内容

# gitee
Host  gitee.com
HostName   gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host  github.com
HostName  github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

4.ssh密钥添加到gitee

ssh-add ~/.ssh/gitee_id_rsa

在任何页面的右上角,单击您的个人资料照片->设置->左侧菜单栏找到SSH公钥->将C:\Users\你的用户名.ssh目录下gitee_id_rsa.pub文件打开复制里面的内容添加到密钥(有可能输入密码,输入就行)。

5.ssh密钥添加到github

在任何页面的右上角,单击您的个人资料照片,然后单击 Settings(设置)。

在用户设置侧边栏中,单击 SSH and GPG keys(SSH 和 GPG 密钥)。

单击 New SSH key(新 SSH 密钥)。

在 "Title"(标题)字段中,为新密钥添加描述性标签。 将C:\Users\你的用户名.ssh目录下github_id_rsa.pub文件打开复制里面的内容添加到密钥粘贴到 "Key"(密钥)字段。

单击 Add SSH key(添加 SSH 密钥)。

如有提示,请确认您的 GitHub 密码。

6.用ssh命令分别测试

6.1 测试gitee

输入以下内容:

$ ssh -T [git@gitee.com](mailto:git@gitee.com)

若返回 Hi XXX! You've successfully authenticated, but 码云 Gitee — 基于 Git 的代码托管和研发协作平台 does not provide shell access. 内容,则证明添加成功。

6.2 测试github

输入以下内容:

$ ssh -T [git@github.com](mailto:git@github.com)

您可能会看到类似如下的警告:

> The authenticity of host 'Build software better, together (IP ADDRESS)' can't be established.
> RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
> Are you sure you want to continue connecting (yes/no)?

或类似如下:

> The authenticity of host 'Build software better, together (IP ADDRESS)' can't be established.
> RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
> Are you sure you want to continue connecting (yes/no)?

验证您看到的消息中的消息之一,然后输入 yes:提示这个表示配置成功

> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.


繁华落尽,雪花漫天飞舞。