前言
一开始,自己想的多了,看了很多文章,而每篇文章都存在些许差异,自己就犯难了。后来突然想明白了,其实Gitee和Github验证的方式完全一样,不就是使用不对称加密来完成验证的吗。想明白了这一点,剩下的流程了就简单了。只需要生成两对公钥和私钥即可。具体操作流程如下。

生成两对公钥和私钥
PS:我是在全新的系统上配置的,即不存在原来相关账户和配置的残留,如果有残留,据说要先解绑(删除配置),具体操作如下:

1
2
git config --global --unset user.name "yourname"
git config --global --unset user.email "yourEmail@163.com"

下面是真正的生成公钥和私钥:

1
2
ssh-keygen -t rsa -C 'yourEmail@163.com' -f ~/.ssh/id_rsa_github
ssh-keygen -t rsa -C 'yourEmail@163.com' -f ~/.ssh/id_rsa_gitee

生成的文件在 C:\Users\用户名.ssh
103171741522.png

把公钥添加到Gitee和Github上
后缀名为 .pub 的为公钥,在各自网站个人信息设置处添加,具体步骤不再赘述。

生成配置文件
主要是告诉Git我在访问这两个网站时,你要知道拿着哪个私钥去验证。
在.ssh目录下,生成config文件(文件使用Windows自带的文本编辑器编辑即可)

1
2
3
4
5
6
7
8
9
10
11
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

测试

1
2
$ ssh -T git@gitee.com
$ ssh -T git@github.com

010317295894.png