Git配置多个SSH-Key
1、生成 gitee SSH-Key;
$ ssh-keygen -t rsa -C 'git@gitee.com' -f ~/.ssh/gitee_id_rsa
2、生成 github SSH-Key;
$ ssh-keygen -t rsa -C 'git@github.com' -f ~/.ssh/github_id_rsa
3、添加配置文件,在 ~/.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 -T git@gitee.com
$ ssh -T git@github.com
5、若需要根据项目设置不同的提交人(user.name),可通过 includeIf 实现配置
# 在 ~/.gitconfig 配置文件中添加 includeIf 和 path
...
[includeIf “hasconfig:remote.*.url:git@gitee.com:your-organization-a/**”]
path = ~/.ssh/gitconfig-gitee-worka
[includeIf “hasconfig:remote.*.url:git@gitee.com:your-organization-b/**”]
path = ~/.ssh/gitconfig-gitee-workb
# 在 ~/.ssh/gitconfig-gitee-worka 中添加
[user]
name="Testa"
email="testa@gitee.com"
# 在 ~/.ssh/gitconfig-gitee-workb 中添加
[user]
name="Testb"
email="testb@gitee.com"
# 通过以上配置,可实现在 a 组织中提交日志的 Author 为 Testa,而在 b 组织中的 Author 为 Testb
内容摘自:gitee.com