GitHub:Windows 上两个帐户的单独凭据

GitHub: Separate credentials for two accounts on Windows

我最近创建了第二个 GitHub 帐户,将我的工作和私人项目分开(以前,我只有工作帐户)。 我将 https 与 Windows 凭据存储结合使用。为了自动 select 正确的帐户,我按照建议 here.

将我的私人帐户信息存储在 ~/.gitconfig 中,将工作帐户信息存储在 ~/work/.gitconfig

不幸的是,当我尝试在我的私有存储库中推送更改时,出现以下错误:

$ git push
remote: Permission to privateuser/privaterepo.git denied to workuser.
fatal: unable to access 'https://privateuser@github.com/privateuser/privaterepo.git/': The requested URL returned error: 403

我按照建议 here 将遥控器 URL 设置为 git remote set-url origin https://privateuser@github.com/privateuser/privaterepo.git。 推送我的工作回购协议仍然可以正常工作。 当我在我的 private/work 回购中键入 git config user.name 时,我分别得到了我的 private/work 用户名 - 应该是这样。

新的私有存储库有什么问题?为什么 git 仍然认为我是 workuser,当我尝试推送到我的私人仓库时?它是否与我用来存储工作凭证的 Windows 凭证存储有关?它从来没有问过我私人账户的密码...

detail here的条件包含仅用于提交作者身份(用户。name/email)。

这与身份验证无关(凭据:username/password)

这些可能缓存在 credential manager (like the linux osx-keychain)
检查输出:

git config credential.helper

如果可以,请在每个环境中使用 SSH 密钥,就像我一样illustrate there然后您可以轻松地为同一个远程仓库维护不同的身份(github.com)


注意:GCM(Git Credential Manager) installed alongside Git for Windows does not, as stated in issue 363,支持每个 URI 多个用户。

您只能通过键入以下内容来为该存储库设置用户:

git config --local user.name 'Full Name'

git config --local user.email 'your@mail.com'

这不会影响您的其他存储库。

我刚刚为我的 Jenkins 设置了一个多凭证设置,它可能适用于您正在做的事情。

对于我们的 Jenkins 用户,我们将我们的配置推送到 AWS CodeCommit 以备份服务器。我们还需要能够使用 mvn release 插件,这需要能够推送到我们的 Github 仓库。我们的 jenkins 实例也在阻止传出 SSH 的子网中,因此我们必须使用 HTTPS。

因此,我们需要两套凭据。还应注意,我们的 Github organizaiton 需要 MFA,因此密码实际上是个人访问令牌。

# /var/lib/jenkins/.gitconfig
[user]
  name = Jenkins
  email = jenkins@domain.io
[credential]
  helper = !aws --profile default codecommit credential-helper $@
  UseHttpPath = true
[credential "https://github.com"]
  helper = store

# /var/lib/jenkins/.git-credentials
https://username:password@github.com/SomeOrg/some-repo

另外一点是,由于您的 repos/organizations 似乎都在 Github 上,这里是 git 文档中可能适用于您的一些其他要点:

useHttpPath - 默认情况下,Git 不认为 http URL 的 "path" 组件值得通过外部助手进行匹配。这意味着为 https://example.com/foo.git will also be used for https://example.com/bar.git 存储了凭据。如果您确实想区分这些情况,请将此选项设置为 true。

https://git-scm.com/docs/gitcredentials