Git HTTPS Token 凭据配置指南

发布时间:2026/5/30 19:07:16

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

相关新闻