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

无需访问Token即可推送至私有GitHub仓库,如何查询缓存凭证?

排查EC2实例无需GitHub令牌即可推送私有仓库的问题及凭证查询方法

问题概述

我创建了私有GitHub仓库,原本认为只有拥有访问权限的用户通过令牌或SSH密钥才能执行推拉操作。已生成GitHub访问令牌,但在Amazon Linux的EC2实例上无需输入令牌就能推送代码,且我的GitHub账户未关联任何SSH密钥。想查询当前环境缓存的GitHub凭证,但执行git config --global credential.helper cache无任何返回。

已执行操作详情

1. 在EC2实例安装Git

dnf install git -y

2. Git全局配置

git config --global user.name "XXX"
git config --global user.email "XXX@XXX.XXX"

(配置的用户名和邮箱与GitHub账户一致)

3. 首次提交及推送

git add .
git commit -m "First commit"
git branch -M main
git remote add origin https://github.com/OWNER/REPOSITORY.git 
git push -u origin main

排查与解决步骤

1. 正确查询Git凭证助手配置

你之前执行的git config --global credential.helper cache是设置缓存型凭证助手的命令,不是查询命令,所以无返回。要查看当前生效的凭证助手,运行以下命令:

# 查看全局级凭证助手配置
git config --global --get credential.helper
# 进入仓库目录,查看本地级凭证助手配置
git config --local --get credential.helper
# 查看系统级凭证助手配置
git config --system --get credential.helper

Amazon Linux默认可能使用store或libsecret作为凭证助手,而非cache,这就是你设置cache没反应的原因。

2. 查找存储的GitHub凭证

根据不同的凭证助手,凭证存储位置不同:

  • 若使用store助手:凭证明文保存在~/.git-credentials文件中,直接查看:
    cat ~/.git-credentials
    
  • 若使用libsecret助手:需要用secret-tool工具查询(先安装工具,若未安装):
    sudo dnf install libsecret-tools -y
    # 搜索GitHub相关凭证
    secret-tool search server github.com
    
  • 若使用cache助手:凭证存在内存中,重启EC2实例后会自动失效,也可手动清除缓存:
    git credential-cache exit
    

3. 排查其他可能的认证途径

  • 检查EC2实例关联的IAM角色:如果实例绑定了IAM角色,且该角色通过GitHub OIDC身份提供商获得了仓库访问权限,也能无需令牌推送。查看实例的IAM角色:
    curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
    
    如果返回角色名称,进一步检查该角色的权限策略是否包含GitHub相关授权。
  • 检查VS Code远程SSH的凭证转发:VS Code的Remote SSH扩展可能开启了代理转发,把你本地的Git凭证同步到了EC2实例。可在VS Code设置中搜索remote.SSH.enableAgentForwarding,确认是否开启了该功能。

4. 强制触发重新认证验证

移除现有远程仓库地址,重新添加后尝试推送,看是否需要输入凭证:

git remote remove origin
git remote add origin https://github.com/OWNER/REPOSITORY.git
git push -u origin main

如果还是无需凭证,可清除现有凭证配置,强制重新认证:

git config --global --unset credential.helper
# 或直接删除存储凭证的文件
rm ~/.git-credentials

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 04:20:08