If you have more than two accounts on GitHub or other Git repositories, you have noticed that it will not let you use the same SSH key for different accounts. This is how you fix that.
Create a new key with a different name
Upload the new key to the service or server
Create an entry for the Host in .ssh/config
Update the .git/config file to use the new ssh identity in the .ssh/config file.
Here's the setup:
The userid = acme
The repository = myopensourcestuff
Step 1:
If you use multiple services, it makes it a little easier to prefix the name with a code: "GH" for GitHub, "GL" for GitLab, "BB" for BitBucket, etc. Use lower case letters for the key but use uppercase letters for the Identity in Step 3.
Generate a new ssh with a different name other than id_rsa or id_dsa but to make it "easier", first cd into the .ssh directory. The important part here is the "-f" parameter. The -f parameter allows you to create a key pair with a specific name.
cd ~/.ssh
ssh-keygen -f ghacme
If you wanted to, you could put a comment at the end of the public key.
ssh-keygen -f ghacme -C "acme@github"
Step 2:
Upload the key(only the public key: the one that ends with .pub) to the server using whichever method you normally do that with. It might be using scp or by logging in to the service and using their facility. With GitHub and GitLab, you have to logon to the site and upload it from your account page.
Step 3:
Create an entry in the .ssh/config file. If you don't have a config file, create it.
The entry should look similar to this; changing the "GHacme" and the "/ghacme" to the ssh key name you created in Step 1.
Host GHacme
HostName github.com
User git
IdentitiesOnly yes
IdentityFile ~/.ssh/ghacme
Step 4:
To be able to use the new Identity to push back to GitHub, you need to make a change to the .git/config file in the directory where your code lives.
Change the entry in the [remote "origin"] section.
Old version:
- [remote "origin"]
url = git@github.com:acme/myopensourcestuff.git
New version:
- [remote "origin"]
url = GHacme:acme/myopensourcestuff.git
A note about cloning repositories
Let's say you fork someones repository and now you want to clone it to your local computer.
Normally, you would issue:
git clone https://github.com/acme/myforkofanotherproject.git
If you have the above Identity setup, change the command to:
git clone GHacme:acme/myforkofanotherproject.git
This will make the correct entry in the .git/config file so you can git push/pull easily.