確認版本
首先確認已安裝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 "[email protected]"
必須洗掉該設定
git config --global --unset user.name "chenfl"
git config --global --unset user.email "[email protected]"
2. 生成新的 SSH keys
1)GitHub 的鑰匙
指定檔案路徑,方便后面操作:~/.ssh/id_rsa.gitlab
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "[email protected]"
直接回車3下,什么也不要輸入,就是默認沒有密碼,
注意 github 和 gitlab 和 gitee 的檔案名是不同的,
2)GitLab 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "[email protected]"
2)Gitee 的鑰匙
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "[email protected]"
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 [email protected]
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 [email protected]
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 的是 [email protected]??,
所以可以這樣測驗:
ssh -T [email protected]
ssh -T [email protected]
ssh -T [email protected]
如果能看到一些 Welcome 資訊,說明就是 OK 的了,如:
chenfl@DESKTOP-VFEC2HJ MINGW64 ~/.ssh
$ ssh -T [email protected]
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 [email protected]
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即可,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375736.html
標籤:其他
