同时使用 Github 以及 Gitee 进行版本管理

通常用法

1
2
3
4
5
6
7
$ ssh-keygen -C "<注释(一般是注册邮箱)>"
$ cat ~/.ssh/id_rsa.pub # 把公钥放到 Github/Gitee 账户的 SSH Key 即可

## 然后可以验证连通性 ##
$ ssh -T [email protected]
# 或者
$ ssh -T [email protected]

同时使用多个账户

首先还是生成 SSH-Key:

1
2
3
$ ssh-keygen -b 4096 -C '[email protected]' -f ~/.ssh/github_ssh_key
$ ssh-keygen -b 4096 -C '[email protected]' -f ~/.ssh/gitee_ssh_key
$ ssh-keygen -b 4096 -C '[email protected]' -f ~/.ssh/github_ssh_key_2

理论上应该可以共用同一对密钥吧,但是这样安全性就……
敲几行复制粘贴几次又费不了多大劲,万一不小心把私钥暴露出去了,损失会小很多。
拿 RSA 密钥对撞库是我未曾设想的道路。

vim ~/.ssh/config 编辑配置文件:

# Default Github User(Github User [email protected])
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_ssh_key

# Another Github User(Github User2 [email protected])
Host github.com-2
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_ssh_key_2

# Default Gitee User(Gitee User [email protected])
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_ssh_key

默认账户没什么好说的,需要注意第二个 github 账户应该使用:

[email protected]:<USERNAME>/<REPONAME>.git

重点是里面的 github.com-2 应该对应 config 配置文件里的 Host
可以自行替换。比如换成 p**nhub.com

设置完成后依次测试连通性:

1
2
3
$ ssh -T [email protected]
$ ssh -T [email protected]
$ ssh -T [email protected]

实际使用

众所周知把本地分支推送到远程应该:

1
2
$ git remote add origin [email protected]:[USERNAME]/[REPONAME].git
$ git push -u origin [branch]

所以同时使用 Gitee 的话:

1
2
3
4
5
6
7
8
9
$ git remote add gitee [email protected]:[USERNAME]/[REPONAME].git

$ git push -u origin main
$ git push gitee main

# 推送其他分支
$ git checkout -b develop
$ git push -u origin develop
$ git push gitee develop
updatedupdated2022-10-252022-10-25