解决 Mac 同时连接Gitlab和Github问题

3,204 阅读1分钟

1、为啥会有这一篇?

今天 发现自己连接 公司 gitlab 后 github 受到了影响,问题关键在于 生成的 钥匙文件 被覆盖,其实 还有一个简单的方式,生成钥匙文件和 gitlab 文件分开 命名 就可解决,不过 有个更好的方式解决

2、思路

  • 1、生成 ssh
mkdir ~/.ssh 
ssh-keygen -t rsa -C "youremail@xxx.com" -f ~/.ssh/id_rsa_gitlab 
ssh-keygen -t rsa -C "youremail@xxx.com" -f ~/.ssh/id_rsa_github

  • 2、连接 github/gitlab
将公钥 复制到 剪切板
pbcopy < ~/.ssh/id_rsa_github.pub
  • 3、只需要将这个 复制到 github ssh 添加位置 并且 命名(随意)

image.png

  • 4、gitlab 同理 不谈了

3、 管理ssh keys

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
  • 创建ssh配置文件
# 创建
touch ~/.ssh/config
# 编辑  注意 编辑完成后 wq 保存
vi ~/.ssh/config
  • 配置模版 (粘贴进去 )
# xxx
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_github
# xxxx
Host gitlab.com
   HostName gitlab.com
   User bgit
   IdentityFile ~/.ssh/id_rsa_gitlab

4、结果&测试连接

ssh -T git@github.com

image.png

  • 大功告成 !

更多 精彩 请访问 主页 ......