Before getting into the professional field of software development, I had always used https for cloning all of my git repositories. It wasn’t until I started juggling three different github accounts between personal, school, and work, that I realized there had to be a better way. In comes secure shell, or SSH for short, to never need to enter in a login password again!
What is SSH?
SSH is a secure protocol and a safe way of administering remote servers such as git repositories like Github.
Check for existing Key
It is a good idea to first check if there is already a default key on your computer by looking at the default folder for a mac where ssh keys are kept running
ls -l ~/.ssh. If one exists already, the default names should be
id_rsa.pub. If neither are there, then you’ll learn how to make one in the next step.
Creating the Key
If you don’t have a key already or want to setup a key for a different email account. Then run the following command entering with the desired email. For simplicity of commands, let’s switch to the
/.ssh directory for now.
cd ~/.ssh ssh-keygen -t rsa -C "your-email-address"
You should then be prompted to enter the name to save the files as, feel free to name what ever you want. To follow suit of the default, I named mine as
Next, you will be asked to enter a passphrase to encrypt the private and public key. If you do not wish to enter one, simply press enter twice to confirm although this is not recommended.
Great, now you just created an SSH key associated for that email account!
Save the SSH Passphrase to Your System
If you created a SSH key with a unique name and don’t want to enter this passphrase from the previous step every time. Then enter the following command.
Now enter the passphrase you just entered in the previous step.
##What are These
id_rsa is your private key for the account and should never leave your machine, as this is what your computer uses to sign any work done for this account.
id_rsa.pub is the public key for the account, this is what you will use to enter on what ever version control system (VCS) you desire, such as github.
Adding Public Key to VCS
Log into whatever version control system you use, for my use case I use GitHub. Typically if you go into the settings, there should be a section for SSH and GPG keys. Click new SSH key and for the title, enter the name of the device you will be able to identify it such as
personal-desktop and then copy-paste the contents of the
id_rsa.pub file into the key section. Finally click add SSH key!
Configuring Private Keys
So now that you’ve created a key and associated it with your appropriate version control, its time to make a config file so your computer can know how to communicate with your version control using the private key. Still in the
.ssh folder, check if a
config file exists with
ls -l. If not not, run:
touch ~/.ssh/config //creates a blank config file atom config //open with atom editor, feel free to use your own editor
Now working in the
config file, lets setup our personal account that we will be using the most to clone repos. Enter the following block of code.
#Default GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa
This will allow you to clone repositories using the SSH urls just like:
git clone email@example.com:username/repo-name
Configure Multiple Emails
Let’s say for example you have a repository that you need to access at work and you want all associated work to be made with that email account. Opening the
config file again, enter the following block of code.
Host github-COMPANY HostName github.com User git IdentityFile ~/.ssh/id_rsa_COMPANY
Now if you ever need to clone a repository from this account, simply replace
git clone git@github-COMPANY:username/repo-name
I recommend setting up work and school emails this way since I only clone one repository from work and a small handful for school.
Now any repos you clone, all work that is commited will be logged as that email and you’ll never need to constantly log into different accounts.