1 清除git全局设置
如果配置第一个账号的时候使用git config --global
设置过,就先要取消掉,否则两个账号肯定会冲突
1 | 取消global |
2 生成新账号的SSH keys
2.1 用 ssh-keygen
命令生成密钥
1 | ssh-keygen -t rsa -C "new email" |
平时都是直接回车,默认生成 id_rsa
和 id_rsa.pub
。这里特别需要注意,出现提示输入文件名的时候(Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_new
)要输入与默认配置不一样的文件名,比如:我这里填的是 id_rsa
和id_rsa_me
。
如果之前没配置过ssh key,这里用不同邮箱生成两遍即可,注意用不同的文件名
成功后会出现:
1 | Your identification has been saved in xxx. |
2.2 添加到ssh-agent中
使用ssh-add
将 IdentityFile 添加到 ssh-agent中
1 | ssh-add ~/.ssh/id_rsa |
2.3 配置 ~/.ssh/config
文件
在~/.ssh/
下新建config
文件
1 | The git info for company |
3 与GitHub链接
复制刚刚生成的两个ssh公钥到对应的账号中
文件id_rsa.pub
中保存的就是 ssh 公钥
1 | pbcopy < ~/.ssh/id_rsa.pub |
在 github 网站中添加该 ssh 公钥
验证是否配置成功,以 github 为例,输入 ssh -T git@github.com
,若出现
1 | Hi xxx! You've successfully authenticated, but GitHub does not provide shell access. |
这样的字段,即说明配置成功。另一个同理。
参考链接:
配置多个git账号的ssh密钥 - 掘金
https://juejin.im/post/5befe84d51882557795cc8f9同一台电脑配置多个git账号 · Issue #2 · jawil/notes
https://github.com/jawil/notes/issues/2