为什么 Git 使用错误的帐户(使用 osxkeychain)?

Why is Git using a wrong account (with osxkeychain)?

我有两个 github 帐户,我将其称为 ActOne 和 ActTwo,以及电子邮件,我将其称为 em@one.net 和 em@two.net。我需要在两个帐户之间切换,但我无法让第二个帐户正常工作。

我当前的项目使用 ActTwo 帐户。当我说 git push 时,我收到此错误消息:

remote: Permission to ActTwo/Tools.git denied to ActOne.
fatal: unable to access 'https://github.com/ActTwo/Tools.git/': The requested URL returned error: 403

我不知道为什么要尝试使用 ActOne 而不是 ActTwo。我当前项目的 .git/config 文件如下所示:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = https://github.com/ActTwo/Tools.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[user]
        email = em@two.net
        name = ActTwo
[credential]
        helper = osxkeychain

我的 ~/git 配置文件如下所示:

[color]
        ui = auto
[filter "lfs"]
        smudge = git-lfs smudge %f
        required = true
        clean = git-lfs clean %f
[credential]
        helper = osxkeychain

以前有这个,但我删除了它,试图解决问题:

[user]
        email = em@one.net
        name = ActOne

附录:

问题似乎是因为我的凭据助手 (osxkeychain) 已将 ActOne 关联到 github URL。我为另一个帐户添加了第二个钥匙串访问条目,但它不起作用。它不起作用的原因是 "Access Control" 选项卡没有 git-credential-osxkeychain 的条目。而且我不能添加一个,因为实际的应用程序在我的 xCode 应用程序包中,而且我无法在 KeychainAccess 中找到进入应用程序包的任何方法。这是正确的方法吗?有人知道怎么做吗?

I don't know why it's trying to use ActOne instead of ActTwo.

因为您的凭据助手 (osxkeychain) 已将 ActOne 关联到 github URL。

打开您的钥匙串访问,搜索 github.com 相关文件 -> 并在那里编辑凭据。
参见“Updating credentials from the OSX Keychain”。

如果您已经拥有与该 HTTPS URL 关联的(您需要的)凭据,请切换到 SSH URL:如 described here,您可以通过这种方式管理多个帐户.


OP MiguelMunoz adds :

I had to do a lot to fix this.

  • I had to remove the GitHub entries from my Keychain.
  • I had to set local GitHub user and email using git config user.email and git config user.name.
  • I had to delete the user and email from ~/.gitconfig.
  • I had to set up two identities in my ~/.ssh/config file.
  • I had to set the remotes to use the new identity by using git remote --set-url.
  • And I had to add IdentitiesOnly yes to each identity in the ~/.ssh/config file.

我遇到了同样的问题,刚刚弄明白了。 在钥匙链中,我有 2 个 github 条目 - 只是删除了有问题的条目。 下次我尝试推送相关存储库时,它再次要求输入密码和电子邮件帐户。 这以正确的方式创建了新条目。 希望这对某人有所帮助。