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 (https://help.github.com/articles/generating-ssh-keys). Usually, you would have a
~/.ssh folder on your machine, and the first SSH keys generated will often be
id_rsa.pub. You need to copy the content of
id_rsa.pub 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 firstname.lastname@example.org:test1/testproject.git
When you press enter, git would automatically check the content of
~/.ssh/id_rsa.pub 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:
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.pub. 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
id_rsa.pub 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
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:
Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa Host github.com-test2 HostName github.com IdentityFile ~/.ssh/id_rsa_test2
In this file, the most important part is Host and IdentityFile. Remember the sample SSH link we have earlier?
email@example.com:test1/testproject.git. The part after
@ character and before
: is called Host. And our config says that if any repos has the link on this
github.com 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:
firstname.lastname@example.org:test1/testproject.git. Simple, right? HostName is used to tell which real host we will communicate with (remember that we have other git servers: bitbucket.org, heroku.com, 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