How to Work With Multiple Github Accounts in One Machine
You’ve probably created your personal Github account for personal use or for your work in your machine and now you want to be able to use both accounts from the same computer. This post explains how to setup multiple Github accounts in a single machine.
Check for Existing SSH Keys
Check for available SSH keys by running:
$ ls -al ~/.ssh
This lists all existing SSH keys. If you see these two files:
id_rsa
id_rsa.pub
That means you have keys available and you can just use those for your personal use and create another pair for your work account.
Generate SSH Keys If You Don’t Have Any
If you don’t have any existing SSH keys, first you need to generate a pair for your personal use:
- Open a Terminal window and run:
This creates a new SSH key pair.
$ ssh-keygen -t rsa -C "your-email-address"
- Press Enter when prompted for the location to save the keys to save them in the default location.
- Type a secure passphrase and confirm.
Generate SSH Keys for Your Work Account
To generate another pair of SSH keys you have to make sure that you don’t overwrite your personal keys. Follow the next steps to do so:
- Open a Terminal window and run:
This generates the SSH keys and saves the public key with your work email as tag and differentiate it from the other pair of keys.
$ ssh-keygen -t rsa -C "your-work-email-address" -f "id_rsa_companyname"
- Press Enter when prompted for the location to save the keys to save them in the default location.
- Type a secure passphrase and confirm.
- Run
$ ls -al ~/.ssh
again to verify that you have two pair of keys created. You should have four files:id_rsa id_rsa.pub id_rsa_companyname id_rsa_companyname.pub
Add the Keys to the Corresponding Github Account
To avoid having to type in the username and password everytime you make a push to Github, you have to make Github trust the keys you have created. To do so,
- Use
$ pbcopy < ~/.ssh/id_rsa.pub
to copy the public SSH key. Usexclip -sel clip ~/.ssh/id_rsa.pub
if you are using Linux. - Log in to your personal Github account.
- Go to Settings.
- Select SSH and GPG keys under the Personal settings menu.
- Click the New SSH key button.
- Give it a title and paste the public SSH key in the text area below.
- Click Add key.
Repeat the steps above on your work Github account. Use pbcopy < ~/.ssh/id_rsa_companyname.pub
.
Add the SSH Keys to the SSH Agent
Because you created keys with unique names, you have to register them with the ssh-agent to use them.
- Start the ssh-agent with:
$ eval "$(ssh-agent -s)"
- Add the keys like so:
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/id_rsa_companyname
Create or Modify the Config File
You have to modify the ~/.ssh/config
file to automatically load the keys into the ssh-agent.
1.If the config file exists in ~/.ssh
modify it.
If it doesn’t exist, create it and make sure it contains the following values:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host github.com-companyname
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_companyname
2.Save the file.
This configuration makes the ssh-agent use id_rsa
as key for any Git URL that uses @github.com
and id_rsa_companyname
whenever a Git URL uses @github.com-companyname
.
Try It Out
You have now configured your SSH keys to work with different Github accounts. You can try it out by cloning or pushing to your different repositories. These are the different ways that you will have to keep in mind while doing so:
When cloning a repository for your personal account you will use:
$ git clone git@github.com:<personalaccountname>/<repositoryname>.git
When cloning a repository for your work account you will use:
$ git clone git@github-companyname:<workaccountname>/<repositoryname>.git
Note that when using your personal account you use git@github.com
and when using the work account you use git@github-companyname
.
That’s it, you’re all done!