確認版本
首先確認已安裝Git,可以通過 git –version
命令可以查看當前安裝的版本。
可以通過命令 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 "chenfl"
git config --global user.email "1571539116@qq.com"
必須刪除該設置
git config --global --unset user.name "chenfl"
git config --global --unset user.email "1571539116@qq.com"
2. 生成新的 SSH keys
1)GitHub 的鑰匙
指定文件路徑,方便后面操作:~/.ssh/id_rsa.gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "1571539116@qq.com"
直接回車3下,什么也不要輸入,就是默認沒有密碼。
注意 github 和 gitlab 和 gitee 的文件名是不同的。
2)GitLab 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "1571539116@qq.com"
2)Gitee 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "1571539116@qq.com"
3)完成后會在~/.ssh / 目錄下生成以下文件
- id_rsa.github
- id_rsa.github.pub
- id_rsa.gitlab
- id_rsa.gitlab.pub
3.添加識別 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
4. 多賬號必須配置 config 文件(重點)
若無 config 文件,則需創建 config 文件
創建config文件
touch ~/.ssh/config
config 里需要填的內容如下
#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
# 其他自己搭建的
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、gitee 網站添加 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 密碼 ,確認后即添加完畢。
6.測試是否連接成功
由於每個托管商的倉庫都有唯一的后綴,比如 Github 的是 git@github.com😗。
所以可以這樣測試:
ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com
如果能看到一些 Welcome 信息,說明就是 OK 的了,如:
chenfl@DESKTOP-VFEC2HJ MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Hi cfl1! You've successfully authenticated, but GitHub does not provide shell access.
chenfl@DESKTOP-VFEC2HJ MINGW64 ~/.ssh
$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
Hi chenfl! You've successfully authenticated, but GITEE.COM does not provide shell access.
結果如果出現這個就代表成功:
- GitHub -> successfully
- GitLab -> Welcome to GitLab
- Gitee -> successfully
7.其他說明
有的時候github拉去速度十分慢可以使用國內鏡像,目前已知Github國內鏡像網站有github.com.cnpmjs.org和git.sdut.me/。速度根據各地情況而定,在clone某個項目的時候將github.com替換為github.com.cnpmjs.org即可。