How to Set Up Two or More GitHub Accounts from 1 computer.

·

6 min read

Intro: Recently, I embarked on a mission to set up a new GitHub account. I wanted to keep my personal projects separate from the GitHub account I earmarked for tutorials, templates, and other projects I wanted to share with my community under a branded account. At the onset, it seemed like this would be an easy project, but it sent me on a three-day trip down a rabbit hole that had me convinced I'd broken my computer.

I'm writing this guide for anyone thinking about setting up another GitHub account for work or personal use. There are so many great articles and tutorials already on this topic that will help you. I'll list the ones that I referenced below. I've also written out my method; it's a combination of what I got from these resources, plus my suggestions for how to test along the way.

Before Starting:

Let's start with the references. I'm writing from the perspective of a Mac user, but some of the articles below will reference Windows commands.

  1. Medium Article Better Programming by Amir Bizimana He's got a great explanation about SSH and breaks down the steps really well.

  2. Medium Article How to have 2 or more GitHub accounts on one machine (Windows) | by Linh Nguyen My

  3. DevTo Article How to use multiple GitHub accounts on a single machine - DEV Community by Jogendra Kumar

  4. YouTube Tutorial - Configure Multiple GitHub Accounts @Infinity (youtube.com/watch?v=zBssUO_5H_A) This is the one that helped me get unstuck!

Tip: This may seem obvious but make sure you use a recent reference before you embark on making any changes.The initial stack overflow post I found was 10 years old, and it wasn't as helpful as the medium articles + YT tutorial that I found later.

For a Mac

Summary: Step 1: Create a new set of SSH keys Step 2: Map new public SSH key to the 2nd GitHub Account Step 3: Register SSH Keys Step 4: Edit the Config File Step 5: Set email address in Git

Step 1: [In terminal] Create a new set of SSH Keys

You may remember when you initialized setup on your GitHub account, generating an SSH key. The SSH allows your computer to "talk" to GitHub; it's a gatekeeper of sorts. Creating an SSH key establishes a pair of keys, one public and one private. The private one stays on your local machine, and the public one is utilized for the GitHub account.

You'll need a pair of SSH keys for each GitHub account you want to set up. To check on your current SSH keys, use this command:

ls -al ~/.ssh

It should generate something that looks like this, with the ssj key ending in rsa:

staff   574 Apr 24  2021 id_rsa
staff   574 Apr 24  2021 id_rsa.pub

To generate a new pair of keys use this command, plug in the e-mail associated with the GitHub account you're adding. If it asks for a passphrase for the new key, you can hit enter to leave it blank or enter a passphrase according to your preference.

ssh-keygen -t rsa -C <example@email.com>

Then use the ls command to display the SSH keys again.

ls -al ~/.ssh

You should now have two pairs of keys. The original and your new one, each with a public and private key.

staff   574 Apr 24  2021 id_rsa
staff   574 Apr 24  2021 id_rsa.pub
staff  2610 Nov  2 17:26 id_rsa_insertAcctName
staff   576 Nov  2 17:26 id_rsa_insertAcctName.pub

Step: 2: Map new pub SSH key to second GitHub account [in GitHub]

Now we want to make sure our second GitHub account has the information for the SSH keys that we just generated.Go to GitHub. Navigate to your profile in the upper right-hand corner, select Settings, and on the left-hand side under the settings menu, select SSH/GPG. Then select New Key.

Profile -> Settings -> SSH and GPG keys -> New Key copy and paste the new public ssh key from the terminal into your GitHub account and hit save.

Step: 3: [In terminal]Register SSH keys

At this point you've generated a new SSH key and mapped it to the new GitHub Account. Before doing anything else you want to register the keys with the ssh-agent.

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_insertAcctName

Step 4: [In terminal] Edit config file map old and new accounts

First, you want to check if you have a config file set up. navigate to

  1. Navigate into the ssh directory
    Cd ~/.ssh
    
  2. List the current files in the directory
    ls
    
  3. If a file exists, use the appropriate command to open in your editor. For VS Code it will be the following.
    code config
    
    Or open the file config with the default editor:
open -t config

if it doesn't, then use the touch command to create a new file.

touch config

Once you have the file open, use this format to map out both accounts. I created one for my branded account unnamedrd, you can replace that information with whatever is relevant to you.

# Account 1 (original) - the default config
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

# Account 2 (personal) 
Host github.com-unnamedrd
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_insertNameHere
    IdentitiesOnly yes

Save file and close.

Step 5: [In terminal] Set email address for Git

I've seen others mark this step as optional. However, GitHub will use the email address attached to your project folder/repo to match the account. So if you skip this step, you may find that you show up as a contributor to your second GitHub account under your original GitHub account login.

To prevent this, first, check the email associated with any repo associated with your second account by running this command in terminal.

$ git config user.email

The terminal will return an email. If that's not the one you want to manage this project with, you can change it with the following command.

$ git config user. email example@email.com

Then run the command once more to make sure that the email was set correctly.

$ git config user.email

If you want to change the email globally, as the default you would use the following. commands.

$ git config —global user.email 
$ git config —global user.email example@email.com

Summary. Now you've got everything in place. The best way to test this is to set up a test repo and go through the git workflow using the information for the new account.

Post Project Notes: I set up the second GitHub account the right way, but I made two errors. In retrospect, the corrective measures for this could be applied to several projects.

1.) Testing often. I didn't plan out tests at appropriate intervals.

2.) I realized I did not have as good a grasp on git workflow and GitHub as I needed to.