git通过https协议使用仓库时如何记住token?
git通过https协议使用仓库时如何记住token?
通过配置 Git 的凭据助手(credential helper),可以记住 HTTPS 访问时需要的用户名和 Token(作为密码),避免重复输入。
常用方法(按安全性/便捷性排序)
1. 临时缓存(适合短期使用)
1git config --global credential.helper cache
2# 默认缓存 15 分钟,可修改超时(秒):
3git config --global credential.helper 'cache --timeout=3600'
2. 永久存储到文件(明文,安全性低)
1git config --global credential.helper store
2# 凭据会保存在 ~/.git-credentials,首次输入后永久生效
3. 使用操作系统安全存储(推荐)
- Windows(Git for Windows 自带)
1git config --global credential.helper manager-core
2# 或旧版:git config --global credential.helper wincred
- macOS(钥匙串)
1git config --global credential.helper osxkeychain
- Linux(需要安装
libsecret)
1sudo apt-get install libsecret-1-0 libsecret-1-dev
2sudo make -C /usr/share/doc/git/contrib/credential/libsecret
3git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
或者使用 gnome-keyring 等。
4. 直接在远程 URL 中嵌入 Token(一次性配置,URL 变更)
1git remote set-url origin https://你的用户名:你的token@github.com/用户名/仓库.git
⚠️ Token 会明文存储在 .git/config 中,且 git remote -v 会暴露 Token。