动态

详情 返回 返回

Git 免密認證:Git Credential Helper - 动态 详情

提到 Git 免密,也許大多數人會想到 SSH。但是當 SSH 不可用的時候怎麼辦?

比如你沒有提交 SSH 公鑰的權限。

又比如自建的 Git,如果所有域名都在 Cloudflare 的保護下,不想暴露 ip 地址,就只能用 https。(PS: 這種情況也可以用 Cloudflare Tunnel,而且需要客户端額外配置,不是很推薦)

Git Credential Helper

Git Credential Helper 就是解決在 HTTPS 協議下 Git 操作時需要頻繁輸入密碼或者 PAT 的痛點。

設置僅需一行代碼:

git config --global credential.helper <helper_name>

macOS

git config --global credential.helper osxkeychain

Windows

新版的 Windows Git 應該自動安裝了 GCM,無需單獨設置。

git config --global credential.helper manager
# 或者
git config --global credential.helper wincred

Linux

如果有桌面環境,可以存到GNOME Keyring 或者 KDE Wallet

git config --global credential.helper libsecret

如果沒有,可以看下面的

通用方法

  1. Cache

    默認在內存中持續15分鐘,如果有需要可以延長時間。

    git config --global credential.helper cache
    # 一小時
    git config --global credential.helper 'cache --timeout=3600'
    
  2. Store

    這種方法不安全,因為是明文存儲,不要在公共環境或者生產環境使用,僅作為最後的手段。

    git config --global credential.helper store
    # 或者指定位置
    git config --global credential.helper 'store --file ~/.my-credentials'
    
  3. GCM: Git Credential Manager

    安裝方法見:

    1. https://github.com/git-ecosystem/git-credential-manager/blob/release/docs/install.md

    2. https://docs.github.com/en/get-started/git-basics/caching-your-github-credentials-in-git

    據稱只要安裝,無需配置即可使用。

    對於Windows 用户來説,只要安裝了 GCM,所有的 WSL 均可分享憑證,很方便。

參考資料:

  1. 7.14 Git Tools - Credential Storage
  2. Git credential helpers

Add a new 评论

Some HTML is okay.