﻿# 如何使用 Git Credential Manager 快速清除 Azure Repos 使用者認證快取

因為我過往建立了數十個不知道何時建立的 `PAT` (Personal Access Token) 金鑰，為了避免有 PAT 不小心外洩，索性就把我個人在 Azure DevOps Services 上的 `PAT` 全部清除，然後所有自動化作業都壞了，還好很容易修理。除此之外，我也修改了個人密碼，結果導致本機要透過 `git clone` 下載 Azure Repos 上面的原始碼也都失效。但重點是，雖然認證失敗，卻不會跳出讓我重新登入，原因是「使用者認證」被快取了，這篇文章我要來說說清除「快取」的方法！

![Git Credential Manager](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/498502025cddec72fdce-169022682-2891ef79-8351-4ceb-bc77-8fd05d7310bd.png)

### 認識 Git Credential Manager 工具

早期沒有 Git Credential Manager (GCM) 的時候，Git 其實有內建一些所謂的 `credential helpers`，各個作業系統平台都有自己的實作，其實有點複雜，維護起來也有點吃力。例如 Windows 使用 `wincred`、macOS 使用 `osxkeychain`、Linux 使用 `gnome-keyring/libsecret` 等等。這幾套都只提供**單因素驗證**(single-factor authentication)，而且僅支援 HTTP 為主的 Git 儲存庫。從 Git Credential Manager 問世之後，就開始提供了**多因素驗證**(multi-factor authentication)，而且支援 Azure DevOps, Azure DevOps Server (或 Team Foundation Server), GitHub, Bitbucket, 與 GitLab 等平台，算是非常的實用且安全。

不過 Git Credential Manager (GCM) 一開始也沒有真正「跨平台」實作，而是分成了兩套：

1.  [Git Credential Manager for Windows](https://github.com/microsoft/Git-Credential-Manager-for-Windows) (GCM)
    
    這套使用 .NET Framework 4.7 開發，只能跑在 Windows 平台，由微軟負責維護開發。
    
2.  [Git Credential Manager for Mac and Linux](https://github.com/microsoft/Git-Credential-Manager-for-Mac-and-Linux) (Java GCM)
    
    這套使用 Java 開發，可以跑在 Mac 與 Linux 平台，不過 Bug 有點多，用的人比較少。
    

後來，微軟又將 [Git Credential Manager for Windows](https://github.com/microsoft/Git-Credential-Manager-for-Windows) 改用 .NET Core 進行開發，改名叫做 [Git Credential Manager Core](https://github.com/GitCredentialManager/git-credential-manager) (GCM Core)，完整支援 Windows, macOS, Linux 三大平台，使用上也比較一致。但過沒多久後，微軟把自家的 .NET Core 改名叫 .NET，所以最後終於正名為 [Git Credential Manager](https://github.com/GitCredentialManager/git-credential-manager) (GCM)，這就是目前最新、最正確的名字，希望以後不要再改了！😅

> 每次講到「改名」這種事，微軟真的是罄竹難書啊～ 😒

### 關於 `git-credential-manager.exe` 執行檔的路徑變更

[Git Credential Manager Core](https://github.com/GitCredentialManager/git-credential-manager) 有內含一個 `git-credential-manager.exe` 執行檔，可以用來管理 Azure Repos 的快取，但是從 [Git for Windows](https://git-scm.com/download/win) 的 `v2.36.0` 之後路徑有所改變，然後從 `v2.39.0` 之後路徑又再改一次。

-   Git >= 2.39.0
    
    ```batch
    "C:\Program Files\Git\mingw64\bin\git-credential-manager.exe"
    ```
    
-   2.36.1 <= Git < 2.39.0
    
    ```powershell
    "C:\Program Files\Git\mingw64\bin\git-credential-manager-core.exe"
    ```
    
-   Git < 2.36.1
    
    ```powershell
    "C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager-core.exe"
    ```
    

雖然路徑一直變，但其實對 Windows 用戶是無感的，你可以用以下命令直接執行 GCM 相關命令：

```bash
git credential-manager
```

### 如何在 Linux 安裝 Git Credential Manager

如果你在 WSL 環境中使用 GCM，路徑就很重要了，你必須額外設定 `credential.helper` 並跟 Windows 共用 GCM 暫存的認證資訊。

在 WSL 這樣設定可以跟 Windows 共用一套 Git Credential Manager (GCM)，需特別注意路徑問題：

-   Git >= 2.39.0
    
    ```bash
    git config --global credential.helper "/mnt/c/PROGRA~1/Git/mingw64/bin/git-credential-manager.exe"
    ```
    
-   2.36.1 <= Git < 2.39.0
    
    ```bash
    git config --global credential.helper "/mnt/c/PROGRA~1/Git/mingw64/bin/git-credential-manager-core.exe"
    ```
    
-   Git < 2.36.1
    
    ```bash
    git config --global credential.helper "/mnt/c/PROGRA~1/Git/mingw64/libexec/git-core/git-credential-manager-core.exe"
    ```
    

如果是在傳統的 Linux 環境，或是容器化環境中，可以透過以下命令安裝 GCM：

```bash
curl -sLO https://raw.githubusercontent.com/GitCredentialManager/git-credential-manager/main/src/linux/Packaging.Linux/install-from-source.sh
sh ./install-from-source.sh
```

然後一段命令就可以設定完成：

```bash
git credential-manager configure
```

### 如何清空使用者認證快取

在 Windows 作業系統下，Git Credential Manager (GCM) 登入時會儲存密碼或 Token 在 Windows Credential Store 之中，你可以透過**控制台**的「認證管理員」(Credential Manager)來管理這些記憶的密碼。不過，Git Credential Manager 還額外儲存了一份「認證快取」的資料 (我不知道儲存在哪裡)，所以當這些記憶的密碼過時的時候，若「認證快取」還有資料存在，就會導致 Git 無法通過 Azure Repos 的認證。

這件事對初學者最困擾的地方，就在於大部分人並不清楚如何**清除認證快取**，而且他也沒有讓我再次登入的機會，基本上就是直接無法 `git clone` 複製遠端儲存庫下來。目前我還沒有找到任何 GUI 介面可以快速清除這些快取，但我有找出如何透過**命令列工具**來進行清空快取！

> 由於是 GCM 現在是跨平台的工具，而且有新舊版本的問題，如有執行檔路徑錯誤的狀況，請自行判斷你的確切路徑！

以下是清除 Azure Repos 使用者認證快取的命令與參數：

```bash
git credential-manager azure-repos clear-cache
```

> 執行完畢會顯示 `Authority cache cleared` 字樣！

### 常用命令整理

1.  查詢目前 `credential.helper` 的設定值
    
    雖然用 `git config credential.helper` 就可以查出目前「套用」的 `credential.helper` 的設定值，但很多時候我們會想知道設定值儲存在哪個**設定檔**下，所以用以下命令可以查到完整的資訊：
    
    ```bash
    git config --get-all --show-origin credential.helper
    ```
    
    顯示結果：
    
    ```
    file:/home/will/.gitconfig      /usr/local/bin/git-credential-manager
    ```
    
2.  取消舊版的所有 `credential.helper` 設定
    
    由於 GCM 實在出現太多版了，當你升級到新版 GCM 之後，你的 `git credential` 命令還是有可能不會使用到新版本，因為 Git 設定值有三個等級，分別是 `system` --> `global` --> `local`，而 Git 出廠設定在 `system` 設定就有預設值了，舊版的 GCM 卻會設定在 `global` 層級。
    
    所以我通常會想辦法把 `global` 層級的 `credential.helper` 設定清空，然後重新設定 GCM 即可：
    
    ```bash
    git config --global --unset-all credential.helper
    git credential-manager unconfigure
    git credential-manager configure
    ```
    

### 關於 `git credential` 命令的用法

我之前寫過好幾篇相關的文章，大家可以抽空看看：

-   [如何透過 Git Credential 管理透過 HTTP 通訊協定存取儲存庫的帳號密碼](/post/2018/05/28/Git-Credential-Howto)
    
    這篇文章較完整的介紹了 Git 內建的使用認證儲存機制，也有介紹到 `git credential` 的三大用法！
    
-   [介紹好用工具：Git Credential Manager for Windows (記憶 Git 常用密碼)](/post/2016/02/01/Useful-tool-Git-Credential-Manager-for-Windows)
    
    這算稍微早一點的文章，有特別提到早期的 GCM for Windows 如何將密碼儲存在 Windows Credential Store 儲存區中 (認證管理員)！
    
-   [透過 HTTP 與 HTTPS 連接 Git 儲存庫時如何記憶常用密碼](/post/2014/05/22/Credential-Store-for-Git-HTTP-HTTPS)
    
    這篇文章分享 `winstore`, `cache` 與 `store` 的用法，當成歷史故事來看吧！ 😅
    

### 相關連結

-   [GitCredentialManager/git-credential-manager](https://github.com/GitCredentialManager/git-credential-manager)
    -   Secure, cross-platform Git credential storage with authentication to GitHub, Azure Repos, and other popular Git hosting services.
-   [Git Credential Manager Docs](https://github.com/GitCredentialManager/git-credential-manager/tree/main/docs)
    -   [Command-line usage](https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/usage.md)
    -   [Azure Repos: Access tokens and Accounts](https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/azrepos-users-and-tokens.md)
-   [How to Upgrade GIT on Ubuntu 20.04 LTS - LinuxCapable](https://www.linuxcapable.com/how-to-install-and-update-latest-git-on-ubuntu-20-04/)
-   [Get started using Git on Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-git)
