Setting up SSL for multiple GIT Accounts (Windows 10)
Software developers may most likely have to work with multiple Git repositories from multiple Git accounts. You’ll most likely have to work with personal and work accounts on the same PC. In an instance like this, it’s best to set up a Secure Sockets Layer (SSL) to authenticate when using the accounts.
This is done using SSH keys. We discussed how to setup an SSH key for a GitHub account in the previous article. In this article, let’s discuss how we can do the same for multiple GitHub accounts and use them in one PC.
Currently I have one account on my PC that’s already setup with an SSH key. Suppose I need to setup another account in the same PC.
Step 1 — Create a new SSH key for the new account
The first step is to generate a unique SSH key for my second GitHub account. Open the terminal and run the following command
ssh-keygen -t rsa -C "your-email-address"
You will be asked where the keys should be stored. Be careful to not over-write your existing key for the first account. Specify the path and give it a new file name, preferably a name you can use to identify it later.
Next you will be asked to enter a passphrase. This is to ensure that no one who has access to your PC can read the .shh
folder and impersonate you. For now, leave it empty and just press Enter twice.
Now your key will be generated, and the key’s random art image will be displayed in the terminal. If you navigate to the location you specified earlier, you’ll see two new files have been created.
id_rsa_hSM
— my private key
id_rsa_hSM.pub
— my public key
Do not share your private key with anyone. You can share your public key with others.
Step 2 : Attach the new key
Open the id_rsa_hSM.pub
file in a text editor, and copy all its contents.
Next, log into your new GitHub account. Navigate to the Settings panel, scroll down and go to “SSH and GPG keys”
Click on “New SSH key”. Add a title and paste the key that you copied. Then click “Add SSH key”.
Upon completion, you’ll see the newly added key in your list of keys under SSH keys.
Step 3 : Create a config file
Now that the keys are set up, we need a way to specify when we wish to push to our first account, and when we should instead push to our second account. To do so, we need to create a config
file.
Navigate to the .ssh
folder, and open a Git Bash terminal. Then Enter the following commands:
To create the config
file:
touch config
Now, open to open the config file, enter:
nano config
Now write the following lines into the file:
First, to setup the first (default) account, paste the following snippet.
#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Next, to setup the second account, paste the following snippet (change it accordingly) directly below the above snippet. Note the I gave github_hSM
as the host instead of github.com
this time.
Host github_hSM
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_hSM
The contents of your file should look like this:
Exit the file by pressing Ctrl + X
, Press Y
, Then Enter
The Config file is now successfully created.
Step 4 : Test it out!
Create a new repository from the second account, and copy the SSH link to clone.
Next, open Git Bash where you want to clone, and enter the git clone
command, paste the link and modify it as follows:
git clone git@github_hSM:gowantha-hSM/testRepo_hSM.git
Note that the git@github.com
has been changed as git@github_hSM
since I used github_hSM
as the host for the second account.
The repository will be successfully cloned.
Conclusion
There comes instances where one needs to use multiple GitHub accounts on the same PC. This article will help you setup SSH keys for multiple Git accounts.