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

.netrc加密后多GitHub账户认证失效问题及解决方法

现已解决 - 重复添加github.com配置

在凭证助手的netrc中添加-d调试标志后,我发现它无法匹配远程URL中<USER>@github.com格式的用户名。

由于看到部分Stack Overflow答案使用.netrc的单行格式,我尝试重复添加machine github.com配置,结果成功了🤦‍♀️

machine github.com
  login PERSONAL-USER
  password PAT1
machine github.com
  login WORK_USER
  password PAT2

使用场景

我遇到了一个棘手的问题:未加密的.netrc文件可正常工作,但加密为.netrc.gpg后就失效了。我多年来一直使用YubiKey实时解密的配置,即git config credential.helper 'netrc -f ~/.netrc.gpg -v'。

近期因工作需求调整配置:

  • 拥有个人GitHub账户
  • 雇主使用GitHub Enterprise Cloud,还有一个工作账户

因此我的.netrc文件需要为同一主机配置两个登录信息:

machine github.com
  login PERSONAL-USER
  password PAT1
  login WORK_USER
  password PAT2

我的个人用户名包含连字符-,工作用户名包含下划线_,这会有影响吗?

奇怪的是,未加密时正常,加密后就无法认证:

  • .netrc ✅ 正常工作
  • .netrc.gpg ❌ 认证失败

未加密的.netrc文件可正常使用,但加密后会提示仓库未找到,示例如下:

$ git push origin :test2
using gpg for GPG operations
Using GPG to open /Users/redacted/.netrc.gpg: [gpg --decrypt /Users/redacted/.netrc.gpg]
gpg: encrypted with rsa4096 key, ID ED826C8A57E12FC3, created 2018-05-27
      "Julie Ng <REDACTED>"
remote: Repository not found.
fatal: repository 'https://github.com/julie-ng/REDACTED.git/' not found

已尝试的其他方法

配置上下文

我尝试指定使用的账户,全局配置文件~/.gitconfig包含以下内容:

[credential]
        helper = netrc -f ~/.netrc.gpg -v
        user = <PERSONAL-USER>
        useHttpPath = true
[credential "https://github.com/<WORK_USER>/*"]
        user = <WORK_USER>
[credential "https://github.com/<WORK_OSS>/*"]
        user = <PERSONAL-USER>
[credential "https://github.com/<PERSONAL-USER>/*"]
        user = <PERSONAL-USER>

在Git远程地址中指定用户

我尝试:

# 个人仓库 - 无效
git remote set-url origin https://<PERSONAL-USER>@github.com/...

但无效;对工作仓库执行相同操作:

# 工作仓库 - 有效?!
git remote set-url origin https://<WORK_USER>@github.com/<WORK_USER>...

工作仓库可正常使用。

清除所有缓存等

  • 使用Mac,但未使用OSX钥匙串,确认其中无相关信息
  • 测试间隙执行git credential-cache exit
  • 测试间隙执行gpg-connect-agent reloadagent /bye

我已绞尽脑汁,有人知道为何未加密的.netrc正常,加密为.netrc.gpg后就失效吗?


内容的提问来源于stack exchange,提问作者julie-ng

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 20:45:02