git同时管理gitee和github
使用场景:
同一个git需要管理gitee和给github,但是gitee和github需要不同的公钥进行连接
生成SSH Key
首先生成gitee的SSH Key,按照官网给出的方法生成就行
ssh-keygen -t rsa -C "[email protected]"//填写你的邮箱
复制代码
接下来一直回车,大概按三次就会成功生成了.然后打开 C:/Users/Administrator
(windows环境下默认位置)找到~/.ssh/id_rsa.pub
用任何可疑打开的编辑器打开id_rsa.pub
复制里面的内容,到gitee里面的SSH Key粘贴就行了.
测试成功与否
ssh -T [email protected]
//出现这段话就说明连接成功
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.
复制代码
生成github的SH Key,注意!
这个时候,要进入到.ssh文件夹中打开Git Bash Here
,然后按照默认方式生成SH Key
ssh-keygen -t rsa -C "[email protected]"//填写你的邮箱
//当出现这段话的时候,要在后面改一下文件名
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
//改成这样
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):id_rsa_github
复制代码
接下来还是和上一次一样一直回车,这个时候在ssh
这个目录已经有四个文件了,分别是
id_rsa
id_rsa.pub //这两个文件是gitee的
id_rsa_github
id_rsa_github.pub //这两个是github的
复制代码
接下来和gitee粘贴SH Key一样,打开id_rsa_github.pub,把里面的内容,粘贴到github中的SSH Key管理中
解决冲突
重要的一步来了
在.ssh
文件夹中新建一个config
的文件,注意这个文件没有后缀
文件名就是config,然后粘贴下面的内容
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
复制代码
测试成功与否
//测试gitee
ssh -T [email protected]
//出现这段话就说明连接成功
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.
//测试 github
ssh -T [email protected]
//出现这段话就说明连接成功
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
//出现这段话说明连接失败
Permission denied (publickey).
// 连接失败解决办法
1.检查config里面的文件名是否正确
2.有时候是因为网络原因,重新测试即可
复制代码
实现原理
没啥原理,就是通过配置config文件,让不同的连接代理到不同的公钥上