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

http://www.arccode.net/config-multi-git-account-and-workspaces.html

使用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 "[email protected]"
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 [email protected]
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 "[email protected]"
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 [email protected]
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 "[email protected]"
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 [email protected]
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 '[email protected]'

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

1
2
3
$ cd ~/workspace/gitlab
$ git config --local user.name 'username'
$ git config --local user.email '[email protected]'

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

1
2
3
$ cd ~/workspace/gitlab_gyenno
$ git config --local user.name 'username'
$ git config --local user.email '[email protected]'

从仓库clone项目

github

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

原本从仓库clone项目的指令是, git clone [email protected]:arccode/littlebird-mybatis-generator-core.git

因为配置了config, 所以使用git会使用host自动查找到[email protected]

gitlab(个人)

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

gitlab(公司)

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

指令修改原因同上

 

执行完以上步骤,执行Git clone一直报下面的错误


git clone 解决Permission Denied (publickey)问题

需要参考下文执行

步骤三的1、2命令

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/github_id_rsa

ssh-add ~/.ssh/gitlab_id_rsa

原文地址:https://blog.csdn.net/sxg0205/article/details/81412921

本地git bash 使用git clone [email protected]:***.git方式下载github代码至本地时需要依赖ssh key,遇到权限不足问题时一般都是SSH key失效或者SSH key不存在,重新创建SSH key一般就可以解决问题;

步骤一、检查本地ssh key是否存在

    1、windows下 开始 -- 搜索框输入 git bash,打开git bash窗口;

    2、git base窗口中输入指令 ls ~/.ssh/ 来检查ssh key是否存在;

    3、如果key不存在则按照步骤二重新生成,ssh key已存在则跳过步骤二,执行步骤三;

步骤二、生成ssh key

    1、继续步骤一的git bash窗口执行指令:

            ssh-keygen -t rsa -b 2048 -C "你自己的邮箱地址"

           修改邮箱地址为你自己的邮箱地址,注意此处邮箱地址前后的双引号为英文格式双引号;

    2、指令执行后页面提示:

           

           Generating public/private rsa key pair.
           Enter file in which to save the key (/c/Users/***/.ssh/id_rsa):

         ***表示你自己的当前登录用户名,不做修改直接回车,会将生成的rsa文件保存为默认名称

         再次回车提示:

         Enter passphrase (empty for no passphrase): 
         Enter same passphrase again: 
         提示设置提交/l拉取代码到Github时需要的密码及确认密码;

         设置密码后再次回车提示Your identification has been saved in.... 即表示ssh key生成成功;

步骤三、添加sshkey至ssh-agent

    1、执行eval "$(ssh-agent -s)"确认ssh-agent处于开启状态,打印pid... 表示启用中;

    2、执行指令ssh-add ~/.ssh/id_rsa 添加ssh key至ssh agent,此步会要求输入步骤二设置的密码;

          需要注意的是此处可能报错:Could not open a connection to your authentication agent,我的解决办法是关掉当前git                  bash窗口,重新以管理员身份运行git bash 即解决问题;

步骤四、添加ssh key至guthub

     1、登录https://github.com/,在页面右上角自己头像右边箭头处右击,弹框中进入setting功能;

            

     2、setting界面右边菜单选择SSH and GPG keys,选择新建SSH keys,

    

保存即可;

步骤五:git clone下载代码

   步骤结束,此时再尝试本地使用git clone方式下载代码即可;
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章