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

GitHub: Separate credentials for two accounts on Windows

我最近创建了第二个GitHub帐户,将我的工作和私人项目分开(之前,我只有工作帐户)。
我将https和Windows凭据存储结合使用。 为了自动选择正确的帐户,我按照此处的建议将我的私人帐户信息存储在~/.gitconfig中,将工作帐户信息存储在~/work/.gitconfig中。

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

1
2
3
$ git push
remote: Permission to privateuser/privaterepo.git denied to workuser.
fatal: unable to access 'https://[email protected]/privateuser/privaterepo.git/': The requested URL returned error: 403

像这里建议的那样,我将远程URL设置为git remote set-url origin https://[email protected]/privateuser/privaterepo.git
推送我的工作库仍然可以正常工作。
当我在私人/工作仓库中输入git config user.name时,我分别得到了私人/工作用户名-应该是这样。

新的私人存储库有什么问题? 当我尝试推送到我的私有仓库时,为什么git仍然认为我是workuser? 它与Windows凭据存储(我用来存储工作凭据)有什么关系吗? 它从不要求输入我的私人帐户的密码...


在此详细说明的条件包括仅用于提交作者身份(user.name/email)。

这与身份验证无关(凭据:用户名/密码)

这些可能缓存在凭证管理器中(例如linux osx-keychain)
检查以下内容的输出:

1
git config credential.helper

如果可以的话,请按照每个环境在每个环境中使用SSH密钥:这样,您可以轻松地为同一远程存储库(github.com)维护不同的身份。

注意:如问题363中所述,与Windows的Git一起安装的GCM(Git凭据管理器)不支持每个Uri多个用户。


我只是为Jenkins设置了多证书设置,该设置可能适用于您正在执行的操作。

对于我们的Jenkins用户,我们将配置推送到AWS CodeCommit以备份服务器。我们还需要能够使用mvn release插件,该插件需要能够推送到我们的Github存储库。我们的jenkins实例也位于防止外发SSH的子网中,因此我们必须使用HTTPS。

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

1
2
3
4
5
6
7
8
9
10
11
12
# /var/lib/jenkins/.gitconfig
[user]
  name = Jenkins
  email = [email protected]
[credential]
  helper = !aws --profile default codecommit credential-helper $@
  UseHttpPath = true
[credential"https://github.com"]
  helper = store

# /var/lib/jenkins/.git-credentials
https://username:[email protected]/SomeOrg/some-repo

还有一点是,由于您的两个仓库/组织似乎都在Github上,因此git文档中的一些其他要点可能适用于您:

useHttpPath-默认情况下,Git认为HTTP URL的"路径"部分不值得通过外部助手进行匹配。这意味着为https://example.com/foo.git存储的凭据也将用于https://example.com/bar.git。如果确实要区分这些情况,请将此选项设置为true。

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


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

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

git config --local user.email '[email protected]'

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