Loading

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
posted @ 2025-10-29 15:15  SmileMachine  阅读(146)  评论(0)    收藏  举报