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

使用Git生成github和gitlab各自的钥匙

注: windows的命令行不太好用, 建议用Windows的朋友安装gitbash

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

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

1
2
$ cd ~
$ cd .ssh

生成github钥匙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ 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钥匙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ 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钥匙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ 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访问不同的仓库使用不同钥匙

1
2
3
$ cd ~
$ cd .ssh
$ vim config

config 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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

测试连接是否成功

1
2
3
4
5
6
7
8
9
10
11
12
$ 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(全局)

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

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

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

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

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

从仓库clone项目

github

1
$ 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(个人)

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

gitlab(公司)

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

指令修改原因同上

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.