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

笑着哭i 提交于 2019-12-23 02:53:22

问题


I have two github accounts, which I'll call ActOne and ActTwo, with emails which I'll call em@one.net and em@two.net. I need to switch off between the two accounts, but I can't get the second account to work.

My current project uses the ActTwo account. When I say git push, I get this error message:

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

I don't know why it's trying to use ActOne instead of ActTwo. My current project's .git/config file looks like this:

[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

My ~/gitconfig file looks like this:

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

It used to have this, but I removed it in a vain attempt to fix the problem:

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

Addendum:

The problem seems to be because my credential helper (osxkeychain) has associated ActOne to the github URL. I added a second entry to Keychain Access for the other account, but it doesn't work. And the reason it doesn't work is that the "Access Control" tab doesn't have an entry for git-credential-osxkeychain. And I can't add one, because the actual application is inside my xCode application bundle, and there isn't any way that I can find in KeychainAccess to go inside an application bundle. Is this the right approach? Does anyone know how to do this?


回答1:


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

Because your credential helper (osxkeychain) has associated ActOne to the github URL.

Open your keychain access, search for github.com related file->and edit credentials there.
See "Updating credentials from the OSX Keychain".

If you have already a credential (that you need) associated with that HTTPS URL, switch to an SSH URL: as described here, you can manage multiple accounts that way.


The OP MiguelMunoz adds in the comments:

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:


I had the same problem, and just figured it out. In the key chain I had the 2 github entries - and just deleted the problematic one. The next time I tried to push the relevant repository it asked for the password and Email account again. That created the new entry the right way. Hope this helps someone.



来源:https://stackoverflow.com/questions/53219826/why-is-git-using-a-wrong-account-with-osxkeychain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!