如果已經決定不使用 HTTPS 的方法,我們可以使電腦和 GitHub 之間建立安全連接 SSH 密鑰。下面的步驟將引導通過生成一個 SSH 密鑰,然後將公鑰上傳到 GitHub 的帳戶。

步驟 1 :檢查 SSH 密鑰

首先,我們需要檢查電腦上現有的 SSH 密鑰。打開的 Git Bash 並執行:

cd ~/.ssh
ls
# Lists the files in your .ssh directory

檢查目錄清單,看看是否有一個文件名為 id_rsa.pub 文件或 id_dsa.pub 。如果沒有這些文件請轉到步驟 2。相反之,如果你已經有一個現有的密鑰對,可以直接跳到步驟 3。

步驟 2 :生成一個新的 SSH 密鑰

輸入下面的代碼來生成新的 SSH 密鑰。我們希望將密鑰是儲存在預設的位置,所以直接按 ENTER。

ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
ssh-add id_rsa

接下來會要求輸入 passphrase,可以不輸入

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

接下來就完成了,如下圖:

Your identification has been saved in /c/Users/you/.ssh/id_rsa.
Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

步驟 3 :新增 SSH 密鑰到 Github 上

執行下面指令複製 SSH 密鑰內容到剪貼簿。

clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

1. 前往 Account Settings

2. 點擊左邊選單 "SSH Keys"

3. 點擊按鈕 "Add SSH key"

4. 在 key 的欄位貼上剛剛複製的密鑰內容

5. 點擊按鈕 "Add key"

6. 輸入 GitHub password

步驟 4 :測試是否成功

使用下面指令測試:

ssh -T git@github.com
# Attempts to ssh to github

第一次執行可能會看到以下內容:

The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

直接輸入 yes 之後,應該就會看到下面完成認證的訊息了!!

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

備註:

如果已經使用 Https clone 過 Repository,可以在 Git Bash 執行下面指令設定使用 SSH 密鑰的方式。

git remote set-url origin git@github.com:user/repo.git
arrow
arrow

    ChouAndy 發表在 痞客邦 留言(0) 人氣()