Better Programming

Advice for programmers.

Follow publication

How to Use Multiple GitHub Accounts With One Computer

How to set up and change SSH keys to use multiple GitHub accounts on one machine

Amir Bizimana
Better Programming
Published in
5 min readJun 29, 2020
Photo by Thais Ribeiro on Unsplash

I currently have two GitHub accounts: a personal and a work one. Obviously, my work computer is set up with my work account. I wanted to figure out how to push to GitHub with my personal account whenever I’m working on side projects via my work computer.

At first glance, for the seemingly simple task of switching between two accounts, the process seemed a little involved. But once you break things down into steps, it’s less daunting.

Quick Note on SSH

SSH (or secure shell) is a network protocol which, among other things, allows one computer to communicate with another over an unsecured network like the internet.

Without encryption, data travels over the web in plain text form. Consequently, it would be easy for someone to intercept valuable data, like user names or passwords, for wicked intent.

SSH allows encryption of your data by way of a tunnel, which permits your computer to securely connect to another.

This is done via the use of SSH keys and public-key cryptography (aka asymmetric cryptography) which are used for authentication purposes between said computers, in order to decrypt the information being shared.

Here’s a great video explaining the concept of asymmetric cryptography further.

SSH is commonly implemented using the client-server model.

Client-server model with asymmetric cryptography

When running any remote commands from our computer(e.g., git push or git pull) to a remote repository (GitHub), it’s this protocol that enables us to do so without having to supply a password and username each time.

Let’s now look at the configuration for it.

Configuration

The configuration can essentially be broken down into four steps:

  1. Generate an SSH key for the new account.
  2. Attach this new key to your GitHub account.
  3. Register the new key with the SSH agent.
  4. Create an SSH config file.

Step 1. Generate an SSH key for the new account

Using the SSH protocol, we’ll have to generate a key for any new account we’d like to use.

Hop onto your terminal, run the following command, and attach your GitHub email address:

$ ssh-keygen -t rsa -C "uremail@gmail.com" 

A new key pair will be generated. At this point, you want to be careful not to overwrite your existing key pair, which would look like the following:

({your home Directory}/.ssh/id_rsa):

Instead, you can copy the directory and modify the file name to include your account name, as in the screenshot below:

You will then be prompted to enter a passphrase. Your new SSH key will be found at a directory similar to this:

{your home Directory}/.ssh/id_rsa_uraccount

Step 2. Attach the new key to your GitHub account

We’ve just created a public key, and now we need to allow our GitHub account to trust it. This allows you to not have to authenticate every time you run a remote command (git push, git pull).

Copy the public key via the following command pbcopy < ~/.ssh/id_rsa.pub and then log in to your personal GitHub account:

  1. Go to Settings.
  2. From the left side navigation, click SSH and GPG keys.
  3. Click on New SSH key, name it (it's a good idea to name it after the computer you’re using it in), and paste the key you previously copied.
  4. Click Add key.

Step 3. Register the new key with the SSH agent

We now need to register our key with the SSH agent. The SSH agent keeps track of user identity keys and passphrases and works as an additional layer of security.

In your terminal, write the command ssh-add and add the path to the files containing your keys. As shown below, we are adding keys for our main account and the second account we just created.

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_uraccount

Step 4. Create an SSH config file

The SSH config file allows us to set configuration rules that will specify when we want to push to our work account versus our personal account. This is done by defining which identity file to use ( the ones we’ve added to our SSH agent ) on which domain.

If the file has already been created, it should be at ~/.ssh/config. But by default, it will not exist, so we’ll need to create it by running the command touch ~/.ssh/config.

You can use any text editor or IDE to open and edit the file. I went with the terminal’s text editor vim. I don’t use it frequently, and it does have a bit of a learning curve, so I tend to use a cheat sheet to navigate through it.

To open the file, use vim config.

# Account 1 (work or personal) - the default config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# Account 2 (work or personal) - the config we are adding
Host github-uraccount
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_uraccount

The top part is the default configuration. It comprises the following:

  • the host(github.com)
  • Its host name (github.com)
  • a user ID (git)
  • its identity file (~/.ssh/id_rsa)

We can basically duplicate the default configuration; we’ll only need to changes two things: the host (github-uraccount) and the identity file (to the key we created earlier, ~/.ssh/id_rsa_uraccount).

And that should do it. Save and close the file using :wq (if you are using vim).

Create a new repository

Now that we’re done with the configuration, let’s push to GitHub with our new account.

In your project’s working directory, start by initializing git using git init then go to GitHub and create a new repository.

When you get to the instructions under Existing Git Repo, you’d normally just copy and paste the commands to your terminal to add the repo as the remote to your local repo, but for this case, we need to replace “github.com” with the host we set up in our config file earlier. So instead of the default host git@github.com, we use git@github-uraccount.

git remote add origin git@github-uraccount:uraccount/repo_name.git

Now you can push to GitHub using, for example, the following:

git add .
git commit -m "Initial commit"
git push -u origin master

Associate your commits with a particular username

For a single repository, if you’d like to change the name associated with your commits, you’ll also need to change your user name and email to reflect the GitHub account you want the repository to be associated with by using git config as shown below (to do it globally, add the flag -g before user).

git config user.name "uraccount"
git config user.email "uraccount@gmail.com"

There we have it! Thanks for reading — I hope this little configuration walkthrough was helpful!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Amir Bizimana
Amir Bizimana

Written by Amir Bizimana

Frontend dev. Based in MTL. Keen on creativity.

Responses (8)