首先確認已安裝Git,可以通過 git –version
命令可以查看當前安裝的版本。
Mac OSX 中都已經安裝了Git。但是,Git的版本未必是最新的。
可以通過命令 git clone https://github.com/git/git
進行更新
Git共有三個級別的config文件,分別是system、global和local
。
在當前環境中,分別對應
%GitPath%\mingw64\etc\gitconfig文件
$home.gitconfig文件
%RepoPath%.git\config文件
其中%GitPath%
為Git的安裝路徑,%RepoPath%
為某倉庫的本地路徑。
所以 system 配置整個系統只有一個,global 配置每個賬戶只有一個,而 local 配置和git倉庫的數目相同,並且只有在倉庫目錄才能看到該配置。
大致思路
,建立兩個密鑰,不同賬號配置不同的密鑰,不同倉庫配置不同密鑰。
1. 清除 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 "你的郵箱"
2. 生成新的 SSH keys
1)GitHub 的鑰匙
指定文件路徑,方便后面操作:~/.ssh/id_rsa.gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "abc@qq.com"
直接回車3下,什么也不要輸入,就是默認沒有密碼。
注意 github 和 gitlab 的文件名是不同的。
2)GitLab 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "abcdef@qq.com"
2)Gitee 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "abcdef@qq.com"
3)完成后會在~/.ssh / 目錄下生成以下文件
- id_rsa.github
- id_rsa.github.pub
- id_rsa.gitlab
- id_rsa.gitlab.pub
3.添加識別 SSH keys 新的私鑰
親測Mac下,新增一個 id_rsa.gitee,沒加進去 也識別到了。 所以此步驟可忽略,如有問題刪除所有密鑰 重新按步驟操作一遍。
默認只讀取 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
4. 多賬號必須配置 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
指定本次連接使用的密鑰文件
5.在 github 和 gitlab 網站添加 ssh
Github

直達地址: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

直達地址:https://gitlab.com/profile/keys
- 登錄 Gitlab
- 點擊右上方的頭像,點擊
settings
- 后續步驟如 Github
Gitee / 碼雲

直達地址:https://gitee.com/profile/sshkeys
- 登錄 Gitee
- 點擊右上方的頭像,點擊
設置
- 后續步驟如 Github
添加過程 碼雲 會提示你輸入一次你的 Gitee 密碼 ,確認后即添加完畢。
6.測試是否連接成功
由於每個托管商的倉庫都有唯一的后綴,比如 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
測試 clone 項目
$ git clone git@gitlab.com:d-d-u/java-xxx.git
Cloning into 'java-basic'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
7.操作過程出現的問題/報錯
tilde_expand_filename: No such user
檢查是否成功的時候,報錯:tilde_expand_filename: No such user .
$ ssh -T git@github.com
tilde_expand_filename: No such user .
解決方法:
此問題是因為寫錯了文件路徑
或者 大小寫沒寫對
,刪除重新配置,或者復制我的改好粘貼進去。