How to manage one only key per each git repository?

git, github, ssh

Solution

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 error means the key you are pushing with is attached to another repository as a deploy key, and does not have access to the repository you are trying to push to.

To remedy this, remove the deploy key from the repository, and attach the key to your user account instead.

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:

Host github1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_repo1

Host github2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_repo2

Instead of using `git@gihub.com:user/repo1`, you would use:

github1:user/repo1

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:

git remote set-url origin github2:user/repo2

That way, a `git push` will use the right key (the one for the `repo2`)

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.

Problem

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)`

Original source

Related problems