How to configure SSH for multiple Github accounts?

Note: I only refer to Github in this post but this method can be applied to any Git servers (such as Bitbucket, Heroku, etc)

If you work with Github for a while, you would realize that there are two kinds of links for you to work the Git repos: HTTPS or SSH. Working with HTTPS is quite straightforward, you just need to enter your Github username + password everytime you make changes to the repos. However, doing this everytime is kind of annoying and you (and I) definitely want to avoid that. We want to have a quick and easy way for us to authenticate with Github and push the code there. That's where SSH comes in.

To be short, SSH link allows you to authenticate with Github via public/private SSH key generated on your machine. In addtion, you can use this method so that multiple computers can access to your private repos without giving them the username and password. This is achieved by adding the public key content on your computer to list of SSH keys on your Github account.

For example, let's assume that I have an account on Github called test1. In order to authenticate my account with Github via SSH, I will first generate SSH keys on my computer ( Usually, you would have a ~/.ssh folder on your machine, and the first SSH keys generated will often be id_rsa and You need to copy the content of and add it onto Github.

After doing this step, you would be able to clone your private repos on Github by running the following command, for example:

git clone

When you press enter, git would automatically check the content of ~/.ssh/ file on your computer and compare with your SSH keys on Github to know if you are authenticated and then clone the project.

However, imagine that you have 2 accounts on Github: test1 and test2. How could you use SSH keys for these two accounts? Of course, you can generate new SSH public/private key and use a new name for these files, ex: id_rsa_test2 and But you would soon realize that git only look for the key id_rsa when it tries cloning a private repos.

Also, you can not be able to add content to SSH keys on test2 account because Github do not allow you to do that. And that makes sense because if it does, how can it know if you want to use test1 or test2 when cloning?

This problem can be solved easily by taking advantage of ~/.ssh/config file. This is a special file which git will inspect first before loading SSH key for checking. If this file is empty or does not exist, git would simply use id_rsa key as usual.

The content of ~/.ssh/config looks like this:

        IdentityFile ~/.ssh/id_rsa
        IdentityFile ~/.ssh/id_rsa_test2

In this file, the most important part is Host and IdentityFile. Remember the sample SSH link we have earlier? The part after @ character and before : is called Host. And our config says that if any repos has the link on this host, the key id_rsa will be used. If, for example, you want to use id_rsa_test2 key when cloning this project, you would need to use the following SSH link: Simple, right? HostName is used to tell which real host we will communicate with (remember that we have other git servers:,, etc).

Is this post helpful to you? If you have any other options or suggestion, please let me know. I am glad to discuss with you further. This is my first post and I know that there would be things need to be improved. Thanks