I use git under two scenarios:
- I use some Github repositories.
- I'm currently working with OpenShift, which uses ssh and git for deployment.
First, I used ssh-keygen
for generating a key which updated at OpenShift site. Such key is stored at ~/.ssh/
and created id_rsa
and id_rsa.pub
.
Then I started cloning a repository from Github, I once did ssh-keygen
again and started pushing, it worked ok. Then I cloned another repository and started having problems:
I got problems when cloning to the second repository. Every time I try to push will show something like:
ERROR: Permission to diegoaguilar/cursoJava.git denied to diegoaguilar/cursoCannibalCreatures. fatal: The remote end hung up unexpectedly
But as it can be seen diegoaguilar/cursoCannibalCreatures
isn't correct as it's another repository.
I even tried removing such repository directory, and cloning it again, same happened.
I already got under ~/.ssh
:
config
:
Host cursoJava
Hostname github.com
User git
IdentityFile ~/.ssh/id_java
Host cursoCannibalCreatures
Hostname github.com
User git
IdentityFile ~/.ssh/id_cannibal
Host openshift
Hostname openshift.com
User git
IdentityFile ~/.ssh/openshift
And so got:
id_cannibal id_cannibal.pub id_java id_java.pub known_hosts
Something like id_openshift
and id_openshift.pub
isn't there but as it's not working, I don't care much now.
I created such files and they're .pub
by ssh-keygen -f <filename>
and gave different pass phrases to each. I added the content of the .pub
's as deploy keys at each Github repository settings.
What am I doing wrong? How is this supposed to work? And, when working at another machine, how to properly obtain these keys, proof it's me and work transparently?
EDIT
Output of git remote -v
:
- For cursoJava repository
origin git@github.com:diegoaguilar/cursoJava.git (fetch)
origin git@github.com:diegoaguilar/cursoJava.git (push)
- For cursoCannibalCreatures
origin git@github.com:diegoaguilar/cursoCannibalCreatures.git (fetch)
origin git@github.com:diegoaguilar/cursoCannibalCreatures.git (push)
As mentioned in "ssh,github,it doesnot work", the trick is to not use the default id_rsa(.pub) names for your public:private keys (because yo can only define one couple of those), but different names.
But that would be only if you were to access those repos as different users
In your case, you are accessing the repos with the same users, and one ssh key should be enough.
See "GitHub help":
This is for using GitHub for two different users.
You then define a
~/.ssh/config
file in which you reference each private keys by their full path:Instead of using
git@gihub.com:user/repo1
, you would use:That uses the key
Host
entry 'github1
' to reference the user (git
), hostname (github.com
) and the exact private/public key to use~/.ssh/id_repo1(.pub)
So if you have a second repo which use a second key stored as
~/.ssh/id_repo2(.pub)
, you need to use the entry 'github2
' (you can name it as you want) defined above, and then change the url you have for origin:That way, a
git push
will use the right key (the one for therepo2
)If you don't, you will be able to push for one repo (using the default key
~/.ssh/id_rsa(.pub)
, default name), but you won't be able to push to the second repo, which need a different set of public/private key.