Multiple SSH keys for different GitHub accounts
Today we’ll show you how to set up multiple SSH keys for different GitHub accounts. When you need to work with the two different GitHub accounts and those accounts need to be set up with an SSH key then you might need to do some settings in config of the SSH to manage multiple GitHub accounts on a single machine.
Manage multiple SSH keys for different GitHub accounts
- Generating a new SSH key and adding it to the ssh-agent
- Adding a new SSH key to your GitHub account
- Modify the SSH config
- Manage the remote URL of the repository
1. Generating a new SSH key and adding it to the ssh-agent
To clone a repository using SSH URL then we have to first generate a new SSH key and add it to the
ssh-agent. Refer to the link below for more information.
You can set up the configuration based on the installed OS.
Note: Please enter a different key name based on the repository name so you can identify it later.
Just assume that we have created two different keys that are listed below.
2. Adding a new SSH key to your GitHub account
Now, let’s link those keys with the GitHub account. For that you have to copy the SSH key and add it in your account settings. Refer to this link for step by step information.
3. Modify the SSH config
So now the most important part is to modify the SSH config to manage multiple GitHub accounts using SSH on a single machine.
Jump into the
.ssh folder to create/modify a
config file. If you can’t see the
config file in the folder then simply create it.
Check out the following lines that I have written into the config file.
In the above code, we have set the two different values (
github.com-work for work repository and
github.com-personal for personal repository) for
Host to be used to add/update the remote origin of the GitHub repositories.
4. Manage the remote URL of the repository
At last, we have to clone a repository using SSH URL but we need to slightly update the repository host name as mentioned above.
To clone work repository:
git clone email@example.com:xxxxxx/xxxx.git
To clone personal repository:
git clone firstname.lastname@example.org:xxxxxx/xxxx.git
If you have already cloned the repositories then run the following command to update the remote URL.
To change remote URL for work repository:
git remote set-url origin email@example.com:xxxxxx/xxxx.git
To change remote URL for personal repository:
git remote set-url origin firstname.lastname@example.org:xxxxxx/xxxx.git
Check out more articles on Git.
That’s it for today.
Thank you for reading. Happy Coding..!!