一台电脑上同时使用github和gitlab

2017-10-11 静,远

ssh钥匙文件默认存储于操作系统当前用户目录的 .ssh 子目录下

为了生成自命名的钥匙, 切换到 .ssh 作为当前目录

$ cd ~
$ cd .ssh

生成github钥匙

$ ssh-keygen -t rsa -C "arccode@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ZL-arccode/.ssh/id_rsa): github_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in github_id_rsa.
Your public key has been saved in github_id_rsa.pub.
The key fingerprint is:
SHA256:MV7M1OCoiiIazgV8D52uNxhs9zi840PbT2CfOFDLhYQ arccode@163.com
The key's randomart image is:
+---[RSA 2048]----+
|     .. ....     |
|    E. + +.      |
|      + = +      |
| .   = = +       |
|  j.= C S        |
|   +** + .       |
|   +oO= +        |
| .ooOi=o         |
| .o=B* o.        |
+----[SHA256]-----+

生成自己gitlab钥匙

$ ssh-keygen -t rsa -C "username@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ZL-arccode/.ssh/id_rsa): gitlab_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitlab_id_rsa.
Your public key has been saved in gitlab_id_rsa.pub.
The key fingerprint is:
SHA256:2zfJw2RePrrGc6qKqIvH3qylCmzimIpiUKKAWB87A98 username@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
|ooo +  o+        |
|+. + = o.o .     |
|. . + + E +      |
|   . + + +       |
|    . . S .      |
|o  .   . .       |
|o+. ...          |
|Xoi=  o..        |
|/B=c+ooo         |
+----[SHA256]-----+

生成公司gitlab钥匙

$ ssh-keygen -t rsa -C "username@company.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ZL-arccode/.ssh/id_rsa): gitlab_gyenno_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in gitlab_id_rsa.
Your public key has been saved in gitlab_id_rsa.pub.
The key fingerprint is:
SHA256:2zfJw2RePrrGc6qKqIvH3qylCmzimIpiUKKAWB87A98 username@company.com
The key's randomart image is:
+---[RSA 2048]----+
|ooo +  o+        |
|+. + = o.o .     |
|. . + + D +      |
|   . + + +       |
|    . . S .      |
|o  .   . .       |
|o+. ...          |
|Xii=  o..        |
|/B=c+ooo         |
+----[SHA256]-----+

之后在 ~/.ssh 目录下将新增6个文件

  • github_id_rsa
  • github_id_rsa.pub
  • gitlab_id_rsa
  • gitlab_id_rsa.pub
  • gitlab_gyenno_id_rsa
  • gitlab_gyenno_id_rsa.pub

打开github和gitlab网页, 将 github_id_rsa.pub , gitlab_id_rsa.pub , gitlab_gyenno_id_rsa.pub 分别配置到对应的 sshkey

注: 此处不上传公钥(.pub)的话, 连接时将提示 Permission denied (publickey).

自定义config文件, 指导本地git访问不同的仓库使用不同钥匙

$ cd ~
$ cd .ssh
$ vim config

config 内容如下

# github
Host github
HostName github.com
User git
IdentityFile ~/.ssh/github_id_rsa
# gitlab(个人) 
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab_id_rsa
# gitlab(公司)
Host gitlab_gyenno
HostName gitlab.gyenno.com
User git
IdentityFile ~/.ssh/gitlab_gyenno_id_rsa

测试连接是否成功

$ ssh -T github

Enter passphrase for key 'C:/Users/ZL-arccode/.ssh/github_id_rsa':
Hi arccode! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T gitlab
Enter passphrase for key '/c/Users/ZL-arccode/.ssh/gitlab_id_rsa':
Welcome to GitLab, username!

$ ssh -T gitlab_gyenno
Enter passphrase for key '/c/Users/ZL-arccode/.ssh/gitlab_gyenno_id_rsa':
Welcome to GitLab, username!

这里的 githubgitlab 是config中配置的host; 根据此host,git可以找到配置对应的地址

配置仓库, 让不同仓库的项目工作在不同目录

笔者配置如下:

  • github工作目录: ~/workspace/github
  • gitlab工作目录(个人): ~/workspace/gitlab
  • gitlab工作目录(公司): ~/workspace/gitlab_gyenno

检查当前配置

git config --list

github配置, 提交时的用户名和email(全局)

$ cd ~/workspace/github
$ git config --global user.name 'arccode'
$ git config --global user.email 'arccode@163.com'

gitlab配置(个人), 提交时的用户名和email(局部)

$ cd ~/workspace/gitlab
$ git config --local user.name 'username'
$ git config --local user.email 'username@gmail.com'

gitlab配置(公司), 提交时的用户名和email(局部)

$ cd ~/workspace/gitlab_gyenno
$ git config --local user.name 'username'
$ git config --local user.email 'username@company.com'

从仓库clone项目

github

$ git clone git@github:arccode/littlebird-mybatis-generator-core.git

原本从仓库clone项目的指令是, git clone git@github.com:arccode/littlebird-mybatis-generator-core.git

因为配置了 config , 所以使用git会使用 host 自动查找到git@github.com

gitlab(个人)

$ git clone git@gitlab:arccode/arccode.net.git

gitlab(公司)

$ git clone git@gitlab_gyenno:backend/GyennoMedicalCloud.git

指令修改原因同上


用户评论
开源开发学习小组列表