- 需要准备两个仓库,我这里使用
github
和gitlab
- 下面分别是两个仓库的不同的账号信息
- github
user.name=xxx
user.email=xxx@xx.com
- gitlab
user.name=yyy
user.email=yyy@yy.com
- 生成私钥和公钥,下面是生成的命令行,生成的私钥和公钥,我放在了
~/.ssh
文件夹下面
ssh-keygen -t rsa -b 4096 -C "注释"
-
将生成的公钥(文件后缀是
.pub
)分别在github和gitlab的SSH密钥中设置好 -
在
~/.ssh
文件夹下面新增一个config
文件,配置如下:
Host Github
HostName github.com
PreferredAuthentications publickey
User xxx@xx.com
IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes
UseKeychain yes
Host Gitlab
HostName gitlab.com
PreferredAuthentications publickey
User yyy@yy.com
IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes
UseKeychain yes
- 创建三个
.gitconfig
文件,内容如下:
~/.gitconfig
[user]
name = "xxx"
email = "xxx@xx.com"
[includeIf "hasconfig:remote.*.url:git@github.com:**/**"]
path = ".gitconfig-github"
[includeIf "hasconfig:remote.*.url:git@gitlab.com:**/**"]
path = ".gitconfig-gitlab"
~/.gitconfig-github
[user]
name = "xxx"
email = "xxx@xx.com"
~/.gitconfig-gitlab
[user]
name = "yyy"
email = "yyy@yy.com"
- 把密钥添加到ssh-agent的高速缓存中,命令行如下:
ssh-add ~/.ssh/id_rsa
- 这样就行了