I googling a lot. but it still report an error after execute 'git push -u origin master'. Finally I execute 'git push -u origin master' success, but I really don't know where the error is. Please read on patiently the content as follow.
1, I've had a git account 'HelenZeng', now I have another account 'Snowbabe'
2, I generate two ssh key, one is id_rsa_helen, another is id_rsa_snow
3, I touch ~/.ssh/config
#github.snowbabe
Host github.com-Snowbabe
HostName github.com
User Snowbabe
IdentityFile ~/.ssh/id_rsa_snow
#github.helenzeng
Host github.com-HelenZeng
HostName github.com
User HelenZeng
IdentityFile ~/.ssh/id_rsa_helen
4, I have tried googling, to be honest, I really don't know why use ssh-add
$ ssh-add ~/.ssh/id_rsa_helen
$ ssh-add ~/.ssh/id_rsa_snow
$ ssh-add -l
4096 SHA256:Ky5cNi8JKMUuguV0Snt4epmwFn2MgAk19nzF3MJujWU
/Users/'username'/.ssh/id_rsa_helen (RSA)
4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E
/Users/'username'/.ssh/id_rsa_snow (RSA)
5, I created a react app project, and I want to push it to gitHub repository( to git account 'Snowbabe').
$ git init
$ git remote add origin git@github.com:Snowbabe/React-study.git
6, Here is my .git/config
[remote "origin"]
url = git@github.com:Snowbabe/React-study.git
fetch = +refs/heads/*:refs/remotes/origin/*
it's correct url
7, Finally step
$ git push -u origin master
ERROR: Permission to Snowbabe/React-study.git denied to HelenZeng.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
Try to Solve:
$ ssh-add -l
4096 SHA256:Ky5cNi8JKMUuguV0Snt4epmwFn2MgAk19nzF3MJujWU
/Users/'username'/.ssh/id_rsa_helen (RSA)
4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E
/Users/'username'/.ssh/id_rsa_snow (RSA)
/** delete all **/
$ ssh-add -D
All identities removed.
/** just ssh-add id_rsa_snow **/
$ ssh-add ~/.ssh/id_rsa_snow
$ ssh-add -l
4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E
/Users/yaxian/.ssh/id_rsa_snow (RSA)
Now, execute 'git push -u origin master'
$ git push -u origin master
Counting objects: 18, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 165.52 KiB | 0 bytes/s, done.
Total 18 (delta 0), reused 0 (delta 0)
To github.com:Snowbabe/React-study.git
* [new branch] master -> master
Successfully.
I think you want to know what
ssh-add
does, and why the first way to usessh-add
failed while the second way succeeded. I'm answering with that assumption in mind.Why
ssh-add
works as it doesssh-add
is used to load specific identities in your ssh agent. You have several identities available to you, and your ssh agent will offer them to any server you try to connect to, in an order I don't fully understand.Under typical circumstances, a given server would only accept one of your identities. For example, let's say I added
id_rsa_server1
to connect to host1, andid_rsa_server2
to connect to host2. When I connect to host1, ssh offersid_rsa_server1
to the server and connects successfully. When I connect to host2, ssh first offersid_rsa_server1
, which fails, then tries the next one,is_rsa_server2
, which now succeeds.Your problem when you loaded the two identities is that
github.com
knows both helen and snow. You want to log in with snow, since that's the account that has permissions on that particular repo, but the ssh layer succeeds with the first accepted key, helen, which doesn't have the required permissions.So when you unload your identities and load only the right one, things work because this times you log in to
github.com
as snow, with the required permissions to do the push.I hope this helps clarify what is going on.
Alternative and recommended solution with
.ssh/config
Because you're connecting to
github.com
by that name, I think your.ssh/config
does not do anything. It would only do something if you connected togithub.com-Snowbabe
as the server name.This should work:
.ssh/config
with User=git
:set your Git repo to use this remote name:
With these settings, your ssh agent will use the right identity file when using that host name: the alias defined in
.ssh/config
specifies the user name (must always begit
ongithub.com
) and identity (Snow's RSA key). You still need tossh-add id_rsa_snow
as before, but this configuration overrides what identity is offered first upon connecting via ssh.Edit: I've now tested this procedure well, in the context of answering a similar question, and corrected some mistakes above in the process.