同一台电脑同时使用gitHub和gitLab
一,生成ssh密钥并配置
分别对gitlab和github生成对应的密钥(默认情况下本地生成的秘钥位于C:/Users/用户名/.ssh/)
1,生成gitlab密钥并配置
-
电脑开始菜单中找到已安装的gitbash并打开,输入命令: ssh-keygen -t rsa -C “公司邮箱地址” 按回车,再按3次enter键,生成对应的gitlab密钥:id_rsa和id_rsa.pub -
将gitlab公钥即id_rsa.pub(Windows下路径为C:/Users/用户名/.ssh/)中的内容配置到公司的gitlab上。

2,生成github密钥并配置
-
在gitbash中输入命令: ssh-keygen -t rsa -C “github邮箱地址” -f ~/.ssh/github_rsa -
生成对应的github密钥:github_rsa 和github_rsa.pub -
将github公钥即github_rsa.pub中的内容配置到自己的github上。操作步骤如下:

3,配置git,访问不同host时使用不同的密钥
-
进入密钥生成的位置(C:/Users/用户名/.ssh/),手动创建一个config文件(注意这个config文件要无后缀)。 -
在新建的config文件里面配置如下内容: Host github.com
port 22
User git
HostName ssh.github.com
PreferredAuthentications publickey
IdentityFile ~\.ssh\github_rsa
Host gitlab.com
port 22
User git
HostName git.dev.sh.ctripcorp.com
User git
PreferredAuthentications publickey
IdentityFile ~\.ssh\id_rsa
公司内网地址,设置HostName时需要注意,复制公司gitlab或者自己的github地址时,需要把"https://"去掉。
 
二,验证是否设置成功
在C:/Users/用户名/.ssh中,右键打开Git Bash Here,分别输入命令:
ssh -T git@github.com
ssh -T git@gitlab.xxx.com
如下图所示则说明配置成功 
|