一个电脑下添加多个gitlab/github账号


首先必须明白:
一个gitlab账号对应一个私钥和**。

 一、新建user1的SSH key
ssh-****** -t rsa -C "[email protected]"
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa): /c/Users/Administrator/.ssh/id_rsa_user1

将.ssh/id_rsa_user1.pub文件打开,复制里面的公钥,到gitlab/github的ssh中
一个电脑下添加多个gitlab/github账号一个电脑下添加多个gitlab/github账号

**添加到SSH agent中
ssh-add ~/.ssh/id_rsa_user1

二、新建user2的SSH key
创建:ssh-****** -t rsa -C "[email protected]"
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa):/Users/Administrator/.ssh/id_rsa_user2
将.ssh/id_rsa_user2.pub文件打开,复制里面的公钥,到gitlab/github的ssh中
一个电脑下添加多个gitlab/github账号一个电脑下添加多个gitlab/github账号

**添加到SSH agent中
ssh-add ~/.ssh/id_rsa_user1


三、编辑/创建.ssh/config
# GitLab.com server
Host user1
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_user1
# GitLab.com server
Host user2
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_user2


四、测试是否成功

如果成功,就进行下一步,否则重试上面几个步骤

五、克隆项目
假设原本的ssh地址是:[email protected]gitlab.com:username1/myproject.git
那么在克隆的时候需要将地址改为:[email protected]user1:username1/myproject.git

同理克隆项目2的时候:
假设原本的ssh地址是:[email protected]gitlab.com:username2/myproject.git
那么在克隆的时候需要将地址改为:[email protected]user2:username2/myproject.git