﻿# 如何透過 Git Credential 管理透過 HTTP 通訊協定存取儲存庫的帳號密碼

一般在使用 Git 存取遠端儲存庫時，大部分的人都使用 SSH 或 HTTP(S) 通訊協定。如果使用 SSH 協定，你可以設定金鑰認證，如此一來不但不用每次都輸入密碼，連線過程還可以全程加密。但是若用 HTTP(S) 的話，就沒那麼方便了，預設每次都需要輸入帳號密碼進行驗證。更有甚者，如果 Git 平台設定了多因素驗證/兩階段驗證，那就更難記憶密碼。還好 Git 內建提供了**認證儲存機制** (Credential Storage)，可以幫你暫存常用的帳號密碼，輕鬆幫你做到自動帳號密碼輸入。本篇文章教你認識有哪些認證管理工具，還有如何管理這些帳號密碼。

要使用 Git 內建的 **認證儲存機制** (Credential Storage)，在不同的作業系統平台會有不同的預設值。例如你在 Windows 平台安裝 [Git for Windows](https://git-scm.com/download/win) 的話，但是 Git 本身，預設安裝過程會自動幫你安裝由微軟進行維護的 [Git Credential Manager for Windows](https://github.com/Microsoft/Git-Credential-Manager-for-Windows) 工具；而在 macOS 底下，如果用 [Homebrew](https://brew.sh/zh-tw/) 安裝 Git 的話，預設則會使用內建的 **osxkeychain** 輔助工具；如果是 Linux 的話，也可以使用內建的 **cache** 或 **store** 模式，不過相對的比較不安全，在 Linux 底下，還是建議使用 SSH 來存取遠端儲存庫的物件會比較好。

### 設定使用認證儲存機制

一般來說，Git 在安裝完成之後，都不太需要額外設定認證儲存機制 (Credential Storage)，你可以直接輸入以下命令，查詢目前使用哪一套輔助工具：

> **git config credential.helper**

像在 Linux 底下，預設 Git 並沒有設定任何認證儲存機制的輔助工具，如果你設定為 **cache** 輔助工具的話，當你輸入認證時，預設會幫你儲存在記憶體中，並且自動快取 15 分鐘，在未來的 15 分鐘以內，都不用重複輸入帳號密碼，此時你可以輸入以下命令進行設定：

> **git config --global credential.helper cache**

**※ 備註**：使用 **cache** 輔助工具，使用時會自動在 **~/.git-credential-cache/** 目錄下建立一個 socket 檔案 (檔名為 **socket=** )

如果你想要自訂快取的時間長度，可以加入 **\--timeout <seconds>** 選項 (預設為 900 秒)，如下範例可設定自動快取 1 小時：

> **git config --global credential.helper 'cache --timeout 3600'**

在 Linux 另一種儲存認證的方式，則是設定為 **store** 輔助工具，這種設定會將認證的帳號密碼儲存在指定的檔案內，不過是以「明碼」的方式保存，所以不太建議這樣使用，但如果為了方便，你也可以參考以下設定命令：

> **git config --global credential.helper 'store --file ~/.git-credentials'**

如果在 Windows 作業系統下，預設 [Git for Windows](https://git-scm.com/download/win) 安裝好之後 **credential.helper** 設定值應該就是 **manager** 或 **manager-core**，此時你不用額外設定什麼，直接就可以使用。這個設定主要是選用 [Git Credential Manager for Windows](https://github.com/Microsoft/Git-Credential-Manager-for-Windows) 作為主要的認證密碼管理工具。不過，事實上所有的密碼會安全的保存在**控制台**的「**認證管理員**」中。如果你的電腦沒有正確設定的畫，也可以透過以下命令進行設定：

> **git config --global credential.helper manager-core**

如果再 macOS 作業系統下，預設也應該會安裝好 **osxkeychain** 輔助工具，這套工具會自動採用 macOS 內建的 [**鑰匙圈存取**](https://support.apple.com/zh-tw/guide/keychain-access/what-is-keychain-access-kyca1083/mac) ([Keychain](https://support.apple.com/guide/keychain-access/what-is-keychain-access-kyca1083/mac)) 工具安全地保存密碼。照理說你在透過 [Homebrew](https://brew.sh/zh-tw/) 安裝 Git 的時候，預設就會自動設定完成。

### 如何預先儲存特定遠端儲存庫的認證資訊

事實上，不同平台下所使用的**認證儲存機制**並不相同，所使用的命令也有點不太一樣，不過它們之間有個很好辨識的規則。

Linux

-   直接使用 **git credential** 即可進行設定，但前提為要先將 **credential.helper** 設定為 **store** 才行。

Windows

-   如果設定為 **manager** 的話，那就直接使用 **git credential-manager** 或 **git credential-manager-core**  進行操作。
-   如果要查詢相關用法，可以輸入 **git credential-manager --help** 命令。

macOS

-   如果設定為 **osxkeychain** 的話，那就直接使用 **git credential-osxkeychain** 進行操作。

接著，我們就可以來學習如何預先建立遠端儲存庫的認證資訊！

這裡我就先用我在 GitHub 上面的 **https://github.com/doggy8088/demo1.git** 遠端儲存庫為例，如果我希望用 **exampleuser1** 身分進行存取，那麼你可以這樣設定。

Linux

基本上 **git credential** 只有三種用法：

-   填入認證資訊：**git credential fill  
    **以下範例示範如何設定一組連往 github.com 的認證資訊，輸入後會請你設定帳號密碼：  
    **cat <<EOF | git credential fill  
    ****protocol=https  
    ****host=github.com  
    ****EOF**

-   通知 credential helpers 核准以下認證資訊：**git credential approve  
    **以下範例示範如何建立一個新的連往 github.com 的認證資訊，加入 path 則代表只有該 **path** 才會送出特定認證資訊  
    **cat <<EOF | git credential approve  
    ****protocol=https  
    ****host=github.com**  
    **path=doggy8088/demo1.git  
    ****username=exampleuser1  
    ****password=YourPa$$w0rd  
    ****EOF**

-   通知 credential helpers 刪除以下認證資訊：**git credential reject  
    **以下範例示範如何刪除所有連到 github.com 的認證資訊  
    **cat <<EOF | git credential reject  
    ****protocol=https  
    ****host=github.com  
    ****EOF**

Windows

-   先建立一個輸入命令檔，假設檔名為 **Input.txt**，而檔案內容如下：  
    **protocol=https  
    host=github.com**
-   然後輸入以下命令可查詢目前「認證管理員」中，是否有以知的認證被保存下來：  
    **type Input.txt | git credential-manager get**
-   如果要建立或刪除特定 GIT URL 的認證資訊，要先準備一個輸入檔，內容如下：  
    **protocol=https  
    ****host=github.com  
    ****path=doggy8088/demo1.git  
    ****username=exampleuser1  
    ****password=YourPa$$w0rd**

-   建立認證資訊  
    **type Input.txt | git credential-manager approve**
-   刪除認證資訊  
    **type Input.txt | git credential-manager reject**
-   另外一種建立認證的方式，則是直接輸入以下命令：  
    **git credential-manager fill  
    **接著輸入兩行內容，並多按下一次 Enter 鍵完成：  
    **protocol=https  
    host=bitbucket.com  
    **畫面上就會自動跳出輸入帳號密碼的視窗，輸入成功後，認證資訊就會記錄下來：  
    [![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/b3ec783bb007fd7b71c1-image_thumb_266.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/3986c5eaab7135d9c5e7-image_3134.png)

macOS

-   先建立一個輸入命令檔，假設檔名為 **Input.txt**，而檔案內容如下：  
    **protocol=https  
    host=github.com**
-   然後輸入以下命令可查詢目前「認證管理員」中，是否有以知的認證被保存下來：  
    **cat  Input.txt | git credential-osxkeychain get**
-   如果要建立或刪除特定 GIT URL 的認證資訊，要先準備一個輸入檔，內容如下：  
    **protocol=https  
    **host=github.com  
    ******path=doggy8088/demo1.git  
    ****username=exampleuser1  
    ****password=YourPa$$w0rd**

-   建立認證資訊  
    **cat Input.txt | git credential-osxkeychain store**
-   刪除認證資訊  
    **cat Input.txt | git credential-osxkeychain erase**

**相關連結**

-   [Git - Credential Storage](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage)
-   [git-credential(1)](https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-credential.html)
-   [Caching your GitHub password in Git - User Documentation](https://help.github.com/articles/caching-your-github-password-in-git/)
-   [macos - How do you reset the stored credentials in 'git credential-osxkeychain'? - Stack Overflow](https://stackoverflow.com/questions/11067818/how-do-you-reset-the-stored-credentials-in-git-credential-osxkeychain)
-   [Git Credential Manager for Windows](https://github.com/Microsoft/Git-Credential-Manager-for-Windows)

-   Secure Git credential storage for Windows with support for Visual Studio Team Services, GitHub, and Bitbucket multi-factor authentication.
