生成对应的密钥对
ssh-keygen -t rsa -C "yourmail@gmail.com"
添加私钥
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
shell中创建配置config文件
touch config
编辑config文件内容
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User name1
# git.XXX.com
Host git.XXX.com
HostName git.XXX.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
User name2
如果之前设置过全局用户名和邮箱,需要unset,shell中执行
git config --global --unset user.name
git config --global --unset user.email
然后在不同的仓库下设置局部的用户名和邮箱,对应的repository,shell中执行
git config user.name "yourname"
git config user.email "youremail"