參考:
配置同時使用 Gitlab、Github、Gitee(碼雲) 共存的開發環境
正文:
下載安裝Git:Downloads git
清除git全局設置(如果之前裝過Git並且配置過的話)
新安裝git可跳過。
若之前對 git 設置過全局的 user.name 和 user.email。
類似 (用 git config --global --list 進行查看你是否設置)
$ git config --global user.name "你的名字"
$ git config --global user.email "你的郵箱"
必須刪除該設置
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的郵箱"
生成新的SSH Keys
1)GitHub 的鑰匙
指定文件路徑,方便后面操作:~/.ssh/id_rsa.gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "你的郵箱"
直接回車3下,什么也不要輸入,就是默認沒有密碼。
注意 github 和 gitlab 的文件名是不同的。
2)GitLab 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "你的郵箱"
3)Gitee 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "你的郵箱"
4)完成后會在~/.ssh/
目錄下生成以下文件
id_rsa.github
id_rsa.github.pub
id_rsa.gitlab
id_rsa.gitlab.pub
添加識別 SSH keys 新的私鑰
默認只讀取 id_rsa,為了讓 SSH 識別新的私鑰,需要將新的私鑰加入到 SSH agent 中
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitlab
$ ssh-add ~/.ssh/id_rsa.gitee
多賬號必須配置 config 文件(重點)
若無 config 文件,則需創建 config 文件
創建config文件
$ touch ~/.ssh/config
config 里需要填的內容
親測可以不縮進,所以方便看,建議縮進。
最簡配置
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa.github
完整配置
#Default gitHub user Self
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa.github
#Add gitLab user
Host git@gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa.gitlab
# gitee
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa.gitee
# other settings
Host git@git.startdt.net
Port 22
HostName http://git.startdt.net
User git
IdentityFile ~/.ssh/lab_rsa.startdt
下面對上述配置文件中使用到的配置字段信息進行簡單解釋:
Host
它涵蓋了下面一個段的配置,我們可以通過他來替代將要連接的服務器地址。
這里可以使用任意字段或通配符。
當ssh的時候如果服務器地址能匹配上這里Host指定的值,則Host下面指定的HostName將被作為最終的服務器地址使用,並且將使用該Host字段下面配置的所有自定義配置來覆蓋默認的/etc/ssh/ssh_config配置信息。Port
自定義的端口。默認為22,可不配置User
自定義的用戶名,默認為git,可不配置
HostName
真正連接的服務器地址PreferredAuthentications
指定優先使用哪種方式驗證,支持密碼和秘鑰驗證方式IdentityFile
指定本次連接使用的密鑰文件
在 github 和 gitlab 網站添加 ssh
Github
Github 添加SSH公鑰
直達地址:https://github.com/settings/keys
過程如下:
- 登錄 Github
- 點擊右上方的頭像,點擊 settings
- 選擇 SSH key
- 點擊 Add SSH key
在出現的界面中填寫 SSH key 的名稱,填一個你自己喜歡的名稱即可。
將上面拷貝的~/.ssh/id_rsa.xxx.pub
文件內容粘帖到 key 一欄,在點擊 “add key” 按鈕就可以了。
添加過程 github 會提示你輸入一次你的 github 密碼 ,確認后即添加完畢。
Gitlab
Gitlab 添加SSH公鑰
直達地址:https://gitlab.com/profile/keys
- 登錄 Gitlab
- 點擊右上方的頭像,點擊 settings
- 后續步驟如 Github
Gitee / 碼雲
碼雲 添加SSH公鑰
直達地址:https://gitee.com/profile/sshkeys
- 登錄 Gitee
- 點擊右上方的頭像,點擊 設置
- 后續步驟如 Github
添加過程 碼雲 會提示你輸入一次你的 Gitee 密碼 ,確認后即添加完畢。
測試是否連接成功
由於每個托管商的倉庫都有唯一的后綴,比如 Github 的是 git@github.com:*。
所以可以這樣測試:
ssh -T git@github.com
而 gitlab 的可以這樣測試:
ssh -T git@gitlab.corp.xyz.com
如果能看到一些 Welcome 信息,說明就是 OK 的了
ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Hi dragon! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitlab.com
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSn.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.
Welcome to GitLab, @dragon!
$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrp+KkGYoFgbVr17bmjeyc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Hi 我是x! You've successfully authenticated, but GITEE.COM does not provide shell access.
結果如果出現這個就代表成功:
GitHub -> successfully
GitLab -> Welcome to GitLab
Gitee -> successfully