一臺電腦上同時使用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方式下載代碼即可;
 

 

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