git, how to set up multiple ssh keys for github repositories

For creating one ssh key for all github repositories, go here

Create ssh keys

Go to .ssh directory under your user root directory (or anywhere you want to store your keys).

cd ~/.ssh/

Here’s one way to create a key. There are manay resources that describe how to create secure keys (tutorial: online)

ssh-keygen -t ed25519 -C "lisajohnson@email.com"

Let’s call the keys id_ssh_repo1 and id_ssh_repo2. The key generation will create a key pair: public and private

~/.ssh/id_ssh_repo1. 
~/.ssh/id_ssh_repo1.pub
~/.ssh/id_ssh_repo2
~/.ssh/id_ssh_repo2.pub

Set up and configure config file

If you don’t have config file, create one and open to configure the repo and key pairing

touch ~/.ssh/config
Host repo1.github.com
        Hostname github.com
        User git
        IdentityFile=/Users/lisajohnson/.ssh/id_ssh_repo1

Host repo2FUN.github.com
        Hostname github.com
        User git
        IdentityFile=/Users/lisajohnson/.ssh/id_ssh_repo2

Replace the lines indicated in bold. The first line repo1.github.com is just a name (alias) so you can make it however you want.

Clone your repositories

When you clone using ssh keys, you will be using the alias specified in the config file.

git clone git@repo1.github.com:ljohnson/repo1.git
git clone git@repo2FUN.github.com:ljohnson/repo2.git

git@[your alias]:[yourGithubUsername]/[yourGithubReponame].git

  • The alias is repo1.github.com
  • The github username is ljohnson (username of the repository)
  • The github repository is repo1

Change remote URL

If you want to change remote origin url,

git remote set-url origin git@repo2FUN.github.com:ljohnson/repo2.git

If error occurs, try to check the verbose

ssh -vT git@github.com