是否有方法缓存 HTTPS 凭证以推送提交?

是否有方法缓存 HTTPS 凭证以推送提交?

技术背景

在使用 Git 进行版本控制时,每次通过 HTTP/HTTPS 协议推送或拉取代码都需要输入用户名和密码,这会带来极大的不便。为了解决这个问题,Git 自 1.7.9 版本起引入了凭证助手(credential helpers)机制,它能够帮助用户缓存或存储凭证,从而避免频繁输入。

实现步骤

使用凭证缓存

  • 临时缓存:可以使用以下命令让 Git 将密码临时缓存在内存中。默认缓存时间为 15 分钟,你可以通过 --timeout 参数设置更长的超时时间。
1
2
3
4
5
6
7
8
9
10
11
# 默认缓存 15 分钟
git config --global credential.helper cache

# 缓存 1 小时
git config --global credential.helper "cache --timeout=3600"

# 缓存 1 天
git config --global credential.helper "cache --timeout=86400"

# 缓存 1 周
git config --global credential.helper "cache --timeout=604800"

永久存储凭证

  • 使用 git-credential-store:该方法会将凭证以明文形式存储在本地文件(.git-credentials)中。
1
2
3
4
5
# 存储在项目目录下
git config credential.helper store

# 存储在用户主目录下
git config --global credential.helper store

不同操作系统的特定凭证助手

  • Mac OS X:若使用 Homebrew 安装 Git,可使用本地的 Mac OS X 密钥库。
1
git config --global credential.helper osxkeychain
  • Windows:从 Git for Windows 2.7.3+ 版本开始,可使用 manager 凭证助手。
1
git config --global credential.helper manager
  • Linux
    • Fedora
1
2
sudo dnf install git-credential-libsecret
git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret
- **Ubuntu**:
1
2
3
4
sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

使用加密的 .netrc 文件(Git 1.8.3+)

  • Windows 系统步骤
    1. 安装 gpg4Win Lite,并将 GPG 安装目录添加到系统路径中,同时复制 gpg2.exegpg.exe
1
2
set PATH=%PATH%:C:\path\to\gpg
copy C:\path\to\gpg\gpg2.exe C:\path\to\gpg\gpg.exe
2. 创建或导入 GPG 密钥,并信任该密钥。
1
2
3
gpg --import aKey
# 或者
gpg --gen-key
3. 安装凭证助手脚本。
1
2
cd c:\a\fodler\in\your\path
curl -o c:\prgs\bin\git-credential-netrc https://raw.githubusercontent.com/git/git/master/contrib/credential/netrc/git-credential-netrc.perl
4. 创建明文的 `_netrc` 文件。
1
2
3
4
5
6
7
8
9
machine a_server.corp.com
login a_login
password a_password
protocol https

machine a_server2.corp.com
login a_login2
password a_password2
protocol https
5. 加密 `_netrc` 文件。
1
gpg -e -r a_recipient _netrc
6. 使用加密文件。
1
git config --local credential.helper "netrc -f C:/path/to/_netrc.gpg -v"

核心代码

以下是一些常用的核心代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 临时缓存凭证(1 小时)
git config --global credential.helper "cache --timeout=3600"

# 永久存储凭证
git config --global credential.helper store

# Mac OS X 使用本地密钥库
git config --global credential.helper osxkeychain

# Windows 使用 manager 凭证助手
git config --global credential.helper manager

# Linux Fedora 使用 libsecret 凭证助手
sudo dnf install git-credential-libsecret
git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret

最佳实践

  • 选择合适的凭证存储方式:若对安全性要求不高且希望操作简便,可选择临时缓存;若希望长期使用且对安全性有一定保障,可使用系统特定的凭证助手或加密的 .netrc 文件。
  • 定期更新凭证:对于使用永久存储方式的凭证,建议定期更新密码或令牌,以提高安全性。
  • 结合 OAuth 或个人访问令牌:使用 OAuth 或个人访问令牌代替普通密码,可提高安全性,且部分服务支持多因素认证。

常见问题

  • 凭证缓存超时问题:若设置的缓存时间过短,可能会频繁要求输入凭证。可根据实际使用情况调整 --timeout 参数。
  • 加密文件解密失败:可能是 GPG 密钥的密码短语输入错误,或者 GPG 配置存在问题。可检查 GPG 密钥和密码短语,并确保 GPG 服务正常运行。
  • 凭证存储文件权限问题:若使用 git-credential-store 存储凭证,需确保 .git-credentials 文件的权限设置正确,避免他人访问。
  • 不同操作系统凭证助手不兼容:某些凭证助手可能只适用于特定的操作系统,需根据操作系统选择合适的凭证助手。

是否有方法缓存 HTTPS 凭证以推送提交?
https://119291.xyz/posts/is-there-a-way-to-cache-https-credentials-for-pushing-commits/
作者
ww
发布于
2025年5月21日
许可协议