在Windows 10 安裝 Git 串連 Sourcetree直到我能 push 上GitHub 為止

Willie
3 min readApr 20, 2019

--

  1. 安裝 Git for windows
    https://git-scm.com/download/win
  2. 下載回來 Git-x.xx.x–64-bit.exe 然後無腦開始 Next…Next…Next…
  3. 執行Git Bash(或是命令提示字元)
  4. 輸入指令
git --version

出現

git version x.xx.x.windows.x
便可認為安裝成功

設定使用者帳號密碼等…

git config --global user.name "your name”
git config --global user.email “username@emailpath.com”

確認是否成功,可以透過輸入

git config --list

確認

git config — list

SourceTree

開始安裝 SourceTree
https://www.sourcetreeapp.com/抓取最新版的 SourceTree 版本
SourceTreeSetup-3.1.2.exe
下載後執行安裝碰到第一個問題

The Mercurial team strongly encourages all users to upgrade to 3.7.3 or later due to a security vulnerability.

簡單來說就是 Mercurial 這個免費的代碼管理工具有個洞存在 SourceTree 基於安全的前提下,希望你把他升級到 3.7.3 以上的版本

透過 Google 搜尋 Mercurial 或是 前往以下網址
https://www.mercurial-scm.org/wiki/Download

下載 3.7.3 以上版本回來安裝即可
(以我為例 此時下載到的是 4.7.1)

安裝後,為了確認我還是移除了 SourceTree 重新安裝一次。
結果還是一樣,視窗通知還是跑了出來。

The Mercurial team strongly encourages all users to upgrade to 3.7.3 or later due to a security vulnerability.

光就這一點來說 windows 上安裝的體驗就是一整個大輸 Mac。

這個部份有兩個解決方法

  1. [Tools] → [Options] → [Mercurial] → [Update Embedded]
    更新 SourceTree 內部嵌入的 Mercurial 版本
    然後點選 [Embedded]
  2. [Tools] → [Options] → [Mercurial] → [System]
    套用你系統中的 Mercurial 版本
[Tools] → [Options] → [Mercurial] → [Update Embedded]

SourceTree + GitHub

先進入到 [Tools] → [Options] → [Authentication] → [Add]

[Tools] → [Options] → [Authentication] → [Add]
選擇 GitHub

一般來說 Preferred Protocol 選擇 HTTPS 就可以了
但本人生性龜毛又喜歡打 Command Line 故選擇了 SSH 想搞死自己就是這樣

驗證的部份可以選擇 OAuth
輸入帳號後點選 [Refresh OAuth Token] 來取得 OAuth Token 是比較簡單的方式 但是我一樣不選 偏偏就是選了 Basic

當按下 [Refresh Password]後就會爆錯

Failed to check login for user [xxxxxxxxxx] Must specify two-factor authentication OTP code.

原因在於我一般重要的帳號都會打開兩步驟驗證,
只好退而求其次選擇了 OAuth 的方式。

(延伸閱讀:在SourceTree中使用GitHub進行兩步驟身份驗證(2FA))

[Tools] → [Create or Import SSH Keys]

重頭戲來了
為了透過 SSH 安全的推送到 GitHub
本機端需要產生 SSH Keys。

PuTTY Key Generator

你應該會看到 PuTTY Key Generator 的 SSH 產生工具視窗
1. 點選 [Generate]
2. 在上方空白區域不斷的移動滑鼠遊標
3. 產生成功後應該會看到如下圖的畫面

4. 點擊 [Save private key] 並*選擇一個安全的資料夾存放你的私鑰

5. 複製 Public key for pasting into OpenSSH authorized keys file 內的所有內容

6. 進入你的 GitHub 設定頁面去新增你的 SSH 公鑰
(一般來說是在 https://github.com/settings/keys )

7. 點擊 [New SSH Key]
Title: 你這把私鑰的名字 可以寫入你的電腦名稱 + 系統 + 日期 會比較好記
Key: 請貼上你剛才在 5. 中所復製下來的所有內容

8. 然後會有點小麻煩 你必須先打開 PuTTY 的 Pageant 來幫你自動加上私鑰讓 SourceTree 跟 GitHub 溝通

[工具]→[啟動SSH代理...]

9. 點擊 [Tools] → [Launch SSH Agent…]
然後你應該會困惑 沒有彈出視窗或是出現東西
把你的眼睛轉移到桌面右下的工具列 找出 Pageant 的圖示 並點擊它

Pageant(PuTTY authentication agent)
Pageant Key List

10. 點擊 [Add Key] 選擇你剛才在步驟 4. 時存放的私鑰位置後 點擊[close]

這樣一來 未來你開啟 SourceTree 時,Pageant 就會自動啟動自動幫你載入 SSH Key 代替你來跟 SourceTree 溝通。

最後,試著去 Clone 一些 Repositories 下來和 push 看看能不能正常工作。

結束

如果我的文章有幫助到你解決問題 請不要吝嗇掌聲
在左下角拍手的按鈕按下去 給我掌聲讓我有動力繼續寫下去

--

--

Willie

just willie, Taiwanese, front-end engineer, speak Traditional Chinese and JavaScript, like the cat🐅🐆🐈, I like other guys call me Big Cat