Git HTTPS Token 凭据配置指南
本文说明 Windows 和 Linux 上使用 Git HTTPS 推送时,如何配置、保存、更新和清理 token 凭据。适用于 GitHub、GitLab、CNB 等使用 HTTPS + token 的代码托管平台。
核心概念
HTTPS 推送不使用 .ssh/config。
SSH 的配置通常在:
text
~/.ssh/config
HTTPS 的账号和 token 由 Git credential helper 管理,例如:
bash
git config --global credential.helper manager
git config --global credential.helper store
git config --global credential.helper 'cache --timeout=2592000'
推荐做法是让 remote URL 保持干净,不把 token 写进仓库地址。
推荐:
bash
https://cnb.cool/meowyyds/go
不推荐:
bash
https://cnb:your_token@cnb.cool/meowyyds/go
原因是 token 写进 URL 后,可能会出现在 .git/config、终端历史、日志或截图里。
CNB 的认证方式
CNB 的 HTTPS Git 认证方式:
text
Username: cnb
Password: 你的 CNB token
注意:
- 用户名固定是
cnb - 密码位置填写访问令牌 token
- 不是填写 CNB 登录密码
- 也不是填写你的 CNB 用户名,例如
meowyyds
仓库地址可以写成:
bash
https://cnb.cool/meowyyds/go
也可以写成:
bash
https://cnb.cool/meowyyds/go.git
Windows 配置
Windows 推荐使用 Git Credential Manager。它会把 token 存到 Windows 凭据管理器里。
查看当前凭据 helper
powershell
git config --global credential.helper
配置 Git Credential Manager
powershell
git config --global credential.helper manager
如果你的 Git 版本较旧,可能需要:
powershell
git config --global credential.helper manager-core
设置干净 remote
powershell
git remote set-url origin https://cnb.cool/meowyyds/go
查看 remote:
powershell
git remote -v
应该看到类似:
text
origin https://cnb.cool/meowyyds/go (fetch)
origin https://cnb.cool/meowyyds/go (push)
第一次推送
powershell
git push
如果弹出认证,填写:
text
Username: cnb
Password: 你的 CNB token
之后 Git Credential Manager 会保存凭据,后续同一机器上通常不需要重复输入。
Windows 清理过期 token
如果遇到:
text
remote: Your Credentials have Expired.
remote: 您的访问凭证已经过期。
fatal: unable to access 'https://cnb.cool/...': The requested URL returned error: 400
说明本机缓存了旧 token,需要删掉旧凭据。
方法一:用 Git credential reject
powershell
@"
protocol=https
host=cnb.cool
"@ | git credential reject
如果之前缓存时带了用户名,也可以指定用户名:
powershell
@"
protocol=https
host=cnb.cool
username=cnb
"@ | git credential reject
然后重新推送:
powershell
git push
认证时重新输入:
text
Username: cnb
Password: 新的 CNB token
方法二:打开 Windows 凭据管理器
powershell
control /name Microsoft.CredentialManager
进入:
text
Windows 凭据
删除和 CNB 相关的项,例如:
text
git:https://cnb.cool
cnb.cool
LegacyGeneric:target=git:https://cnb.cool
删完后重新执行:
powershell
git push
方法三:命令行查看 Windows 凭据
powershell
cmdkey /list | findstr /i cnb
如果找到相关 target,可以删除:
powershell
cmdkey /delete:对应的Target名字
注意:Git Credential Manager 的凭据不一定都能通过 cmdkey 完整列出。删不干净时,优先使用 Windows 凭据管理器图形界面。
Linux 配置
Linux 上常见三种方式:
manager:使用 Git Credential Manager,推荐,但需要系统已安装。store:明文保存到文件,最简单,但安全性较低。cache:只缓存在内存中,过期后需要重新输入。
查看当前凭据 helper
bash
git config --global credential.helper
方案一:Git Credential Manager
如果系统已经安装 Git Credential Manager:
bash
git config --global credential.helper manager
然后设置干净 remote:
bash
git remote set-url origin https://cnb.cool/meowyyds/go
推送:
bash
git push
认证时填写:
text
Username: cnb
Password: 你的 CNB token
方案二:store 明文保存
bash
git config --global credential.helper store
第一次推送:
bash
git push
输入:
text
Username: cnb
Password: 你的 CNB token
凭据会保存到:
text
~/.git-credentials
文件内容类似:
text
https://cnb:your_token@cnb.cool
注意:这是明文 token,不推荐在多人机器、服务器、CI 镜像或不可信环境中使用。
建议至少限制权限:
bash
chmod 600 ~/.git-credentials
方案三:cache 临时缓存
缓存 30 天:
bash
git config --global credential.helper 'cache --timeout=2592000'
缓存 1 天:
bash
git config --global credential.helper 'cache --timeout=86400'
这种方式不会长期落盘,但过期后需要重新输入 token。
Linux 清理过期 token
通用清理方式
bash
printf "protocol=https\nhost=cnb.cool\n\n" | git credential reject
如果之前带了用户名:
bash
printf "protocol=https\nhost=cnb.cool\nusername=cnb\n\n" | git credential reject
然后重新推送:
bash
git push
重新输入:
text
Username: cnb
Password: 新的 CNB token
如果使用 store
编辑或删除:
bash
~/.git-credentials
查看是否有 CNB:
bash
grep cnb.cool ~/.git-credentials
删除整条 CNB 凭据后,再重新推送。
也可以直接删除整个文件:
bash
rm ~/.git-credentials
注意:这会清掉所有通过 store 保存的 HTTPS 凭据。
如果使用 cache
退出 credential cache:
bash
git credential-cache exit
然后重新推送并输入新 token。
常用排查命令
查看 remote:
bash
git remote -v
查看全局 credential helper:
bash
git config --global credential.helper
查看当前仓库 credential helper:
bash
git config --local credential.helper
查看所有 credential 相关配置:
bash
git config --show-origin --get-all credential.helper
测试推送:
bash
git push
推荐配置总结
Windows
powershell
git config --global credential.helper manager
git remote set-url origin https://cnb.cool/meowyyds/go
git push
认证时:
text
Username: cnb
Password: CNB token
Linux 桌面环境
优先:
bash
git config --global credential.helper manager
如果没装 Git Credential Manager,又想省事:
bash
git config --global credential.helper store
chmod 600 ~/.git-credentials
如果不想长期保存 token:
bash
git config --global credential.helper 'cache --timeout=2592000'
服务器或 CI
不建议使用全局明文 token。
更推荐:
- 使用 CI 平台的 Secret / Variable
- 运行时注入 token
- 不把 token 写入仓库
- 不把 token 写入镜像
- 不把 token 打进日志
临时推送可以使用:
bash
git push https://cnb:你的token@cnb.cool/meowyyds/go HEAD:main
但这可能进入 shell history,不推荐长期使用。
遇到 CNB token 过期时的标准处理流程
- 生成新的 CNB token
- 确认 remote 没有写死旧 token
bash
git remote -v
- 清理本地旧凭据
Windows:
powershell
@"
protocol=https
host=cnb.cool
"@ | git credential reject
Linux:
bash
printf "protocol=https\nhost=cnb.cool\n\n" | git credential reject
- 重新推送
bash
git push
- 重新输入认证信息
text
Username: cnb
Password: 新的 CNB token