﻿# 透過 HTTP 與 HTTPS 連接 Git 儲存庫時如何記憶常用密碼

不知道各位在 Windows 平台使用 Git 的時候有沒有這種困擾，每次要 git push 的時候都要不斷的輸入帳號密碼，我覺得經年累月之下對工作生產力的損失其實還蠻大的。我用 SSH 協定連接 Git 儲存庫時，都會先設定好 SSH Key 並上傳 Github 或 Bitbucket 免除不斷輸入帳密的問題，但用了 https 為主的連接方式時 ( Visual Studio Online )，預設就不支援了，今天這篇文章就來告訴大家如何設定 http / https 也能記憶密碼的方法。

首先，透過 SSH 連接 Git 儲存庫不用輸入密碼的方式，可以參考我在 [30 天精通 Git 版本控管](https://github.com/doggy8088/Learn-Git-in-30-days)系列文章中的【[第 30 天：分享工作中幾個好用的 Git 操作技巧](https://github.com/doggy8088/Learn-Git-in-30-days/blob/master/zh-tw/30.md)】這篇，裡面有圖文解說，有需要的人可以看看。

其實透過 HTTP / HTTPS 連接 Git 儲存庫，有好多種方法，不同作業系統平台也有不同的用法，以下我們就來分別說明：

從 Git 1.7.9 版本開始，Git 工具有個 **HTTPS Credentials Caching** 功能，它提供一套機制能夠幫助你記憶透過 http / https 連接 Git 儲存庫時所需要輸入的密碼。

在 **Linux** 平台下的 Git 命令，預設有提供一個名為 “cache” 的認證輔助方法(authentication helper)，可以幫你記憶輸入的密碼 15 分鐘，快取 (記憶) 的時間還可以自訂，而且密碼不會儲存到硬碟，算是一個兼具安全與便利性的方法。

你可以透過以下指令啟用這個設定 ( 預設在第一次輸入密碼後會快取 900 秒，也就是 15 分鐘 )：

```
git config --global credential.helper cache
```

如果要設定密碼快取 1 個小時，可改用以下指令：

```
git config --global credential.helper 'cache --timeout 3600'
```

如果要設定永遠快取密碼，則可執行以下指令進行設定：

```
git config --global credential.helper store
```

**請注意**: 如果使用 store 認證輔助方法，帳號密碼將會以**明碼**的方式儲存在 ~/.git-credentials 檔案中。

很可惜，在 **Windows** 平台下的 Git 命令，預設並不支援 cache 認證輔助方法，但支援 store 方法，因此你如果輸入以下指令，依然可以**永久**記憶密碼 ( 不過較不安全 )：

```
git config --global credential.helper store
```

**請注意**: 如果使用 store 認證輔助方法，帳號密碼將會以**明碼**的方式儲存在 %USERPROFILE%\\.git-credentials 檔案中，例如：C:\\Users\\*User1*\\.git-credentials

**\[ 以下內容已過時，請改看新版文章: [介紹好用工具：Git Credential Manager for Windows (記憶 Git 常用密碼)](/post/2016/02/01/Useful-tool-Git-Credential-Manager-for-Windows.aspx) \]**

還好，在 CodePlex 上面有個 [Windows Credential Store for Git](https://github.com/git-ecosystem/git-credential-manager) 專案，他提供一個實作 git credentials API 的工具，可以跟 [Git for Windows](http://git-scm.com/) 工具整合在一起，並且提供在 Windows 平台上更為安全的快取密碼方式。其安裝與設定的方式如下：

1\. 到 [https://github.com/git-ecosystem/git-credential-manager](https://github.com/git-ecosystem/git-credential-manager "http://gitcredentialstore.codeplex.com/") 點選右邊的 download 下載此工具執行檔  
   ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/ff73b858935a3c3faed7-image_2493.png)

2\. 開啟命令提示字元，並執行 git version 確認可以正常執行 git 命令  
   ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/40cdbc1d9a5e737c1de7-image_2494.png)

3\. 直接執行 **git-credential-winstore.exe** 進行安裝  
   ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/521e6be12db033bef6b8-image_2495.png "image")

4\. 你可透過以下指令檢查該程式真正幫你安裝的實體路徑，以及幫你設定的 credential.helper 選項

```
git config --global credential.helper
```

設定完成後，下在你在執行 git push 時，他就會自動開啟 Windows 內建的「**Windows 安全性**」視窗，並且讓你輸入帳號密碼儲存。

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/ac15efcfc3ed8ae58fb8-image_2496.png "image")

比起你去設定 store 認證輔助方法，用這個工具儲存的密碼安全多了！因為這些密碼並不是用明碼儲存，而是儲存在 Windows 作業系統內建的「**認證管理員**」之中 (可以從**控制台**中找到)：

[![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/b33143e2dc7f37c107e7-SNAGHTML5210aea_thumb.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/d994eec8bf2f5ea89d2d-SNAGHTML5210aea.png)

如果你想要移除先前記憶過的密碼，也可以直接從「**認證管理員**」移除特定網站下的密碼即可：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/56c3d6f66c14bcbd4bf8-SNAGHTML52437d7.png)

**相關連結**

-   [git-credential-cache(1) Manual Page](http://git-scm.com/docs/git-credential-cache)
-   [gitcredentials(7) Manual Page](https://git-scm.com/docs/gitcredentials)
-   [git-credential-winstore ( Windows Credential Store for Git )](https://github.com/git-ecosystem/git-credential-manager)
-   [Permanently authenticating with Git repositories - Stash latest (3.0) - Atlassian Documentation](https://confluence.atlassian.com/display/STASH/Permanently+authenticating+with+Git+repositories)
