Git HTTPS Token 凭据配置指南

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 上常见三种方式:

  1. manager:使用 Git Credential Manager,推荐,但需要系统已安装。
  2. store:明文保存到文件,最简单,但安全性较低。
  3. 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 过期时的标准处理流程

  1. 生成新的 CNB token
  2. 确认 remote 没有写死旧 token
bash 复制代码
git remote -v
  1. 清理本地旧凭据

Windows:

powershell 复制代码
@"
protocol=https
host=cnb.cool

"@ | git credential reject

Linux:

bash 复制代码
printf "protocol=https\nhost=cnb.cool\n\n" | git credential reject
  1. 重新推送
bash 复制代码
git push
  1. 重新输入认证信息
text 复制代码
Username: cnb
Password: 新的 CNB token
相关推荐
深海鱼在掘金2 天前
Git 完全指南 —— 第1章:Git 概览与版本控制演进
git
程序员mine2 天前
HTTPS-TLS加密与证书完全指南(中)
网络协议·https·ssl
之歆2 天前
现代 HTTP 客户端深度解析:Fetch 与 Axios
chrome·网络协议·http
noravinsc2 天前
关于Git Flow
git
蜜獾云2 天前
在Git中配置用户名和密码
git
酉鬼女又兒3 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2503 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
程序员mine3 天前
HTTPS-TLS加密与证书完全指南(上)
网络协议·https
scx_link3 天前
通过git bash在本地创建分支,并推送到远程仓库中
开发语言·git·bash
VidDown3 天前
视频帧率技术详解:从 24fps 到 120fps,帧率如何影响你的观看体验?
网络·网络协议·编辑器·音视频·视频编解码·视频