We need two pairs of ssh keys to connect to two different Github accounts.
Check if you have the ssh keys created in the .ssh folder
$ cd ~/.ssh
$ ls -al
If you find a pair of files with name 'id_rsa' and 'id_rsa.pub' or similar, then you already have a pair of ssh keys which we can use.
If not, follow the steps below to create one
$ cd ~/.ssh
$ ssh-keygen -t rsa
Accept the defaults when asked by pressing enter and you'll have a pair of ssh keys with the names given above.
We'll use this pair for our personal account.
Now to create another pair, we'll use a different name. Use the command below to create the second pair.
$ cd ~/.ssh
$ ssh-keygen -t rsa -f "id_rsa_work" -C "your_name@work_email.com"
Here, we create another pair but with different name, id_rsa_work, where we specify work in the name to make it clear it is going to be used for the work account.
Now, you must be having two pairs of ssh keys
id_rsa
id_rsa.pub
id_rsa_work
id_rsa_work.pub
Now, we need to add these two keys to our two different Github accounts. id_rsa.pub to the personal account and id_rsa_work.pub to our work account.
To do so, follow the below steps to add key to your Personal account:
- Go to github.com
- Click on your profile button in the top right corner, and choose Settings
- From the left menu, choose SSH and GPG keys
- Click on New/Add SSH key
- Give a title to the key, 'Personal Laptop', and paste the contents of the id_rsa.pub key. Mind it .PUB key.
Repeat these steps to add the key in your work account and make sure to copy the contents of the id_rsa_work.pub key.
Add the private keys from the pairs to your ssh-agent. Before adding, make sure your ssh-agent is running.
$ ssh-add ~/.ssh id_rsa
$ ssh-add ~/.ssh id_rsa_work
Next, we create a config file in the .ssh folder that will take care of using the correct pair of keys when we use either of the two accounts.
Create a config file.
$ cd ~/.ssh
$ touch config
Open the config file with your favourite editor.
Paste the following code in the file
# Personal Account
Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa
# Work Account
Host github.com-work
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_work
Here, we're configuring to use the First block if the host we are accessing matches 'github.com' else use the second block.
To make the host different, all we have to do is, when cloning the repositories (using ssh) from our WORK account, replace the host of the repo with github.com-work, for example,
When you click the clone button on github and choose using SSH, github gives us a similar snippet
git@github.com:YourUsername/YourRepoName.git
All we need to do is replace the 'github.com' with 'github.com-work' when cloning the repositories from the WORK ACCOUNT.
This way, we change the host of the repository, and while accessing the repository through SSH, the configuration of the second block, we just defined in the config file, will be used.
Also, you may be having repositories already cloned or have origin added. To make them work with your new ssh keys, we need to update the remote origin of the repositories.
Also, make sure you're using the SSH version of the clone since https won't make use of your ssh keys.
To update the origin for your WORK REPOSITORIES, do the following:
- Go to your remote repository
- Click on clone and choose 'using SSH'
- Copy the snippet
- Open your terminal and navigate to the directory of your project
- Run the command
git remmote set-url origin ____
- Paste the snippet in the blank space and UPDATE THE HOST to ...@github.com-work...
Now, one more issue you might need to solve is that of the username used in the commits you make. It is obvious to forget while cloning the repositories to update email which is local to your repository.
To solve this for future, I'd suggest to unset your global email using the below command
$ git config --global --unset user.email
Now, whenever you try to make a commit in your new repository, git will ask you to set your email. Now you can mindfully set the email according to the account the repository belongs to using the below command
$ git config user.email me@workemail.com
That's it. Now go on and create two different repositories in your Personal and Work account and follow the steps to verify you did it correctly.