DEV Community

Tirth Patel
Tirth Patel

Posted on

Using multiple GitHub accounts on one computer

For this article I will use example of two accounts but it could very easily we expanded to multiple accounts.

First step is to create ssh-keys which could be done by the following commands:

ssh-keygen -t ed25519 -C "your_email@personal.com"
ssh-keygen -t ed25519 -C "your_email@work.com"
Enter fullscreen mode Exit fullscreen mode

Note that -C "your_email@personal.com" will just add your email address at the end or the public key. -C option is to add comment at the end of the public key. So it could be anything you like.

Also note -t ed25519 is specifying algorithm used to create the public and private keys. ed25519 or Edwards-curve Digital Signature Algorithm is the 4th generation protocol for ssh key generation. If you don't set this option it will use the RSA algorithm by default to generate the key pairs.

Now you will be prompted with:

Enter file in which to save the key (/home/{username}/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

I would recommend you give a personalized name with location like /home/{username}/.ssh/personal and same goes for work one /home/{username}/.ssh/work
In your home directory you will see .ssh folder which should have personal.pub and work.pub which contains your public key and personal,work which contains your private key.

Now you need to start the ssh-agent using, the following command:

eval $(ssh-agent)
Enter fullscreen mode Exit fullscreen mode

Note that we do eval because ssh-agent once outputs bunch of variables that could be set as environment variables. This just makes life easier so its a good practise. You can check this link for more reference.

Now you could add the private keys to the ssh-agent, so that you don't have to put your passphrase every-time you do anything.

ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter your passphrase for this. You can check if your keys are added by doing

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Now you need to add the public keys to your GitHub accounts. This is a good link for reference.
Go to GitHub can click on your profile on top right hand corner and got to settings.
There if you scroll down you will see an option New SSH key. Click that green button and add the appropriate key. You can copy the key by

pbcopy < ~/.ssh/personal.pub
Enter fullscreen mode Exit fullscreen mode

You have copied the contents of the personal.pub file. You can paste this to your GitHub account as new ssh-key. You can give it a Title in GitHub to better remember it.

Now just hit Add SSH key button and finally you have added the public key to your GitHub. Assuming that you have added both the keys to the respective account let's set up the ssh and git configs to work with both the accounts.

Go to .ssh in your home directory. Check if you have an existing config file. If you don't have one create one. You need to create one that looks like

# Default github account: work-account
Host github.work.com
   HostName github.work.com
   IdentityFile ~/.ssh/work
   IdentitiesOnly yes

# Other github account: personal-account
Host github.com
   HostName github.com
   IdentityFile ~/.ssh/personal
   IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Note that the lines starting with # are just comments and does not affect the config. The flag used IdentitiesOnly is used for ssh-agent to point at the specific key provided by IdentityFile.

Now we want to separate our git configs. Here you could choose your style. For me I like to use my work account everywhere except the ~/personal folder. Here I am going to use a minimal .gitconfig to avoid confusion but go fancy with it.

In you home dir if you don't have one already create a .gitconfig file.

[user]
    email = your_email@work.com
    name = Your Name


[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig-pers
Enter fullscreen mode Exit fullscreen mode

In this file we are setting your default Name, Email and setting a condition that if you are in ~/personal/ it will use load gitconfig from ~/personal/.gitconfig-pers.

Now we have to create the ~/personal/.gitconfig-pers.

[user]
    email = your_email@personal.com 
        name = Your Name
Enter fullscreen mode Exit fullscreen mode

This file will override the email and name when in ~/personal.
Note that the changes will apply to the directories inside the ~/personal/. Which means to check if you config worked create a test folder like so ~/personal/test.
Here if you type

git config --get user.email
Enter fullscreen mode Exit fullscreen mode

You will get your personal email. Except for sub-folders inside ~/personal/ you will get your work email when running the same command.

Enjoy your new setup!

Top comments (0)