【筆記】Source Tree 加入 SSH Key 的方法(適用 GitHub Enterprise)

本篇文章以 Windows 10 與 Source Tree 3.4.5 版本為例。

產生 SSH Keys

  1. Source Tree 選單列 Tools > Create or Import SSH Keys > 點擊 [Generate] 按鈕讓它跑。
    • Parameters 預設值 RSA / 2048 bits 不需調整。
  2. 等待一段時間後,生成 Public Key 與 Private Key。點擊 [Save public key] 與 [Save private key] 將鑰匙存在電腦裡。
    • 建議 Private Key 設定密碼以防有心人士拿到 Private Key 檔案可直接使用。

GitHub 加入公鑰

  1. 進入 GitHub,點擊右上角自己的小頭像會出現下拉選單,點擊 <Settings> 進入設定頁面。
  2. 進入設定頁面後,Personal settings > SSH and GPG keys > 點擊 [New SSH key] 按鈕並將剛剛生成的 Public Key 貼上來儲存。

Source Tree 加入私鑰

  1. Source Tree 選單列 Tools > Launch SSH Agent… 並找到你剛剛儲存的 Private Key。
  2. 大功告成!

Source Tree 與 GitHub 綁定 SSH 後,就可以直接連接 GitHub Enterprise 內的專案了。

Source Tree 連接 GitHub Enterprise 專案可能遇到的信任問題

在 Source Tree Clone 時,將 GitHub Enterprise 專案的 SSH Git Path 貼上後,欄位下方出現 This is not a valid source path / URL。點擊 [Details…] 發現錯誤訊息長這樣:

The server's host key is not cached in the registry. You have no guarantee that the server is the computer you think it is.
 The server's rsa2 key fingerprint is:
 ssh-rsa 2048 -----------------(馬賽克)-----------------
 If you trust this host, enter "y" to add the key to PuTTY's cache and carry on connecting.
 If you want to carry on connecting just once, without adding the key to the cache, enter "n".
 If you do not trust this host, press Return to abandon the connection.
 Store key in cache? (y/n) y

看起來其實沒有什麼錯誤,只是要求我確認是否信任這個連線。但 Source Tree 並沒有讓我輸入 y 的地方!看起來只要沒有一次連成功,Source Tree 會將任何 Output 訊息都放在  [Details…] 純文字的彈窗裡,不管是不是要等 User 互動的訊息!

解決方法:先自己用 Terminal 下指令信任連線。

  1. Source Tree 選單列 Actions > Open in Terminal
  2. 在 Terminal 輸入以下指令:
"<Your Source Tree Path>\SourceTree\app-3.4.5\tools\putty\plink.exe" <Your SSH Git Path>

因為新版 Source Tree 在安裝時無法自行選擇安裝路徑,所以大家的的指令應該會長這樣:

"C:\Users\<Your User Name>\AppData\Local\SourceTree\app-3.4.5\tools\putty\plink.exe" git@github.<Your Company Name>.com:<Your Reop Name>.git

然後就會跳出跟剛剛在 Source Tree 一樣的訊息,這時輸入 y 並 enter,就 OK 了。

接下來回到 Source Tree 重新 Clone 專案,就認證成功囉。

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments