Git ERROR: Permission to user1/repoA.git denied to

2019-02-20 02:04发布

问题:

I have two github accounts and I am trying to configure github, my environment and ssh keys in order that I'm able to push to any repo whether it lies in my github.com/user1 or github.com/user2 account. I have spent hours attempting to get this right following all the resources I can find, alas I seem to be lacking the understanding to get this sorted out. Can anyone help me get this configures correctly?

Each of my two accounts on github.com is set up with different email addresses; github.com/user1 = 'user1@user1.com and github.com/user2 = user2@user2.com.

I have two generated SSH keys the first in .ssh/id_rsa & .ssh/id_rsa.pub and this SSH key is saved in github.com/user1. The 2nd SSH key in .ssh/user2 .ssh/user2_rsaand saved ingithub.com/user1`.

I have a .ssh/config file which contains:

Host user1.github.com
 HostName github.com
 IdentityFile ~/.ssh/user1
Host user2.github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa

When i run ssh-add -l I see both keys are 'loaded'.

When i run ssh -T git@user1.github.com i get Hi user2! You've successfully authenticated, but GitHub does not provide shell access.

When i run ssh -T git@user2.github.com i get Hi user2! You've successfully authenticated, but GitHub does not provide shell access.

I can successfully push to a repo in 'github.com/user2'.

I cannot push to a repo in 'github.com/user1', with below error message;

ERROR: Permission to user1/repo.git denied to user2.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

My `.gitconfig' file contains:

[user]
    name = my full name 
    email = user1@user1.com
[GitHub]
   user = user1

From what I can see it seems that my local environment, cmd, has me stuck as user2 and will not allow user2 to commit to the user1 github account. But a) i thought the script in .ssh/config should recognise that when i'm pushing to user1 it should use the user1 ssh and visa versa, but this does not seem to be happening and b) my .gitcofig has my user.email and my GitHub user as user1 which conflicts with my being authenticated as user2 after running ssh -T ....

I've tried so many things my knickers are in a proper twist on this one now. Can anyone hel get me set up properly with this and or guide me as to how I can erase all SSh's and start over and set up correctly (note that I have a Heroku account set up also that needs to be configured).

Thanks in advance.

回答1:

The 2nd SSH key in .ssh/user2 .ssh/user2_rsa

Yet, your .ssh/config uses id_rsa for user2.github.com

Host user2.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
                      ^^^^^^

And user1.pub public key seems to be registered on user2 account, since a ssh -T git@user1.github.com returns a Hi user2!

Check your local config, and check also on github side which public key has been added to which account.