You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code搭配GitLens时每次Git Push都需身份验证?

强制Git/VS Code每次HTTPS推送时要求身份验证

1. 彻底清除Git凭据缓存配置

先确保全局、仓库级和系统级的凭据助手都被禁用,避免残留配置导致缓存:

  • 清空全局凭据助手配置:
    git config --global credential.helper ""
    
  • 检查并清空当前仓库的本地凭据配置(如果存在):
    git config --local --unset credential.helper
    
  • 检查并清空系统级凭据配置(需管理员权限):
    git config --system --unset credential.helper
    
  • 设置缓存超时为0,强制每次操作重新验证:
    git config --global credential.helper 'cache --timeout=0'
    

2. 禁用Git Credential Manager (GCM) 持久存储

如果你的环境使用GCM(Windows默认),需单独配置禁用持久缓存:

  • 强制GCM不保存凭据:
    git config --global credential.gcm.persist never
    
  • 关闭Windows凭据管理器集成:
    git config --global credential.gcm.useWindowsCredentialManager false
    
  • 设置GCM缓存超时为0:
    git config --global credential.gcm.cacheTimeout 0
    

3. 调整VS Code 相关设置

VS Code可能会独立处理凭据,需同步配置:

  • 打开VS Code设置(快捷键Ctrl+,),搜索git credential helper,将其设置为空。或者直接在settings.json中添加:
    "git.credentialHelper": ""
    
  • 禁用GitHub认证缓存:搜索github caching,将github.caching.enabled设置为false(若有该选项)。
  • GitLens依赖Git本身的凭据配置,无需额外设置,只要Git配置生效,GitLens推送时会自动触发验证。

4. 验证配置有效性

执行以下命令检查所有凭据相关配置,确保无持久化助手残留:

git config --list --show-origin | grep credential

完成后尝试推送代码,此时应该会弹出凭据输入提示(终端或系统弹窗),每次推送都需要验证。

备选方案:使用无凭据的远程URL

确保仓库远程URL未嵌入凭据(如个人访问令牌):

git remote set-url origin https://github.com/你的用户名/仓库名.git

这样每次推送时Git会主动要求输入用户名和个人访问令牌(PAT),完全避免缓存。

内容的提问来源于stack exchange,提问作者Berna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:35:17