The problem we faced usually when you deal with version control, is that, you probably have multiple github accounts and want to maintain various repositories accordingly. Say you have two github accounts: git_A and git_B, and you create repositories repo_a and repo_b under these two accounts.

Github usually will suggests you to generate your public key and associate them with your account. If you’re not familiar with ssh-keygen for github, please refer to this post.

Now you need to generate two different public keys for git_A and git_B. This is what you expect when follow the ssh-keygen:

YOURNAME$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/YOURNAME/.ssh/id_rsa):

This is where you name your public key, e.g., Soon you will have two public keys in /Users/YOURNAME/.ssh/

Now let’s associate with your git accounts. Open your ssh config with any editor you like:

YOURNAME$ vi ~/.ssh/config

And add two sections accordingly in the config file:

#github-A accounts (default)
User git
IdentityFile ~/.ssh/id_rsa_git_A
#github-B account
User git
IdentityFile ~/.ssh/id_rsa_git_B

The name after Host can be any thing you like to identify your git account, obviously they are associated with different private keys here. Now you can test it with ssh, e.g.:



They should be automatically using corresponding public key now, and you will get successful message response.

To init and config a repository locally, you probably will do:

YOURNAME$ git init
YOURNAME$ git config git_A
YOURNAME$ git config ""
YOURNAME$ git remote set-url origin
YOURNAME$ git pull origin
That’s it! now you can do any git commands you want accordingly with different ssh keys.