Easy Steps to Use Multiple GitHub Accounts on Your Mac

Introduction

Recently I got my new Mac, and I was pretty excited. It felt like entering a whole new world of sleek design and smooth functionality. But soon, I hit my first roadblock—I had no clue how to manage multiple GitHub accounts. As a developer juggling both personal and work projects, I needed a way to switch between accounts seamlessly. After a bit of frustration (and some Googling), I finally figured it out, and now I’m here to share what I learned!

Steps

In this article, I will be guiding you on how I went about it using the following steps.

Step1: Setup Your SSH Keys

  1. Open your terminal. You can locate your terminal by using the command + space key; this will open the spotlight and then you can type the word “terminal“ and click enter. Another way you can do this is by clicking the launchpad and searching for the terminal.

  2. Generate. a new SSH key by running the command below

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

    Replace ”<example@email.com>” with the email linked to the GitHub account.

  3. This will prompt you to enter a file path for your generated private and public keys. I like to copy out the path that was displayed at the prompt and add -<special_identifier> to differentiate it from other generated public and private keys in the ~/.ssh directory.

    In this case, I used -personal as the special word to differentiate between other GitHub accounts; for some, it could be -personal, -work,-professional etc.

  4. Next, you’ll be asked to enter a passphrase, but this is optional; you can just hit the return key to skip this step.

  5. If you have other GitHub accounts, you can repeat steps 2-4 for other GitHub accounts.

Step 2: Add the SSH key to GitHub

First, we copy the SSH public key to our clipboard by running this command:

pbcopy < ~/.ssh/<public-key-file>.pub

The public key file is usuallyid_rsa*.pub. It. could also be whatever name we have given it, but it must have the .pub extension.

Next, we add the SSH public key to our GitHub account using the following steps:

  1. Log in to your GitHub account. Remember, it must be the one(s) associated with the email used in generating the keys.

  2. Navigate to your profile picture, which is at the top right corner, and select “Settings”.

After clicking that, on the right-hand side, you’ll see one of the tabs labeled SSH and GPG keys

  1. Click on the New SSH Key button, give a descriptive title, and add the key

  2. Repeat this step for other GitHub accounts.

Step 3: Create and configure SSH file

  1. Go back to your terminal and navigate to the~/.sshdirectory.

     cd ~/.ssh
    
  2. Create the SSH configuration file using the command below

     nano config
    

    Add the following for each of the GitHub accounts:

     # Account 1
     Host github.com-personal
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa
    
     # Account 2
     Host github.com-work
       HostName github.com
       User git
       IdentityFile ~/.ssh/other_key_1
    
     # Account 3
     Host github.com-others
       HostName github.com
       User git
       IdentityFile ~/.ssh/other_key_2
    

Ensure that the, github.com-personal,github.com-work,github.com-othersis replaced with whatever name suits you, i.e github.com-<name>. save and close the config file using the commandcontrol + sandcontrol + x.

  1. Next, remove any existing identities by running the command.

     cd ~
     ssh-add -D
    
     # Next, add the generated keys
     ssh-add -l
    
     # Authenticate the keys with GitHub using the command
     ssh -T github.com-personal
     ssh -T github.com-work
     ssh -T github.com-others
    

    Hurray 🥳, we are done. Now let us test what we have done, by seeing how to use it.

Usage

For a brand-new repository, we will use the command below

# Navigate to the desired location where you want to clone the repo
cd /path/to/folder

# Clone the repo using the following command
git clone git@github.com<special-identifier>:username/repo.git

NB: Ensure that the special-identifier is replaced with the identifier you want to use for the GitHub account, the username with the GitHub username, and repo with the name of the repository.

For existing repositories

# First, we check the remote of the existing repo
git remote -v

# Result: git@github.com:{repo-owner-name}/{repo-name}.git

# We reset to our user
git remote set-url origin git@github.com-{identifier}/{repo-owner-name}/{repo-name}.git

Conclusions

Managing multiple GitHub accounts on a Mac might seem tricky at first, especially if you're new to macOS like I was. But with the right setup and a little patience, you can easily switch between personal and work projects without breaking a sweat. Whether you’re using SSH keys, configuring git configs, or a combination of both, the key is finding the method that works best for your workflow. Hopefully, this guide saves you some of the headaches I had, and now you can focus more on your code and less on account management!

To receive an update when the article drops, do well to subscribe to my newsletter or follow me on any of the social media platforms.

Twitter 🐦: @nwokporo_ebuka

LinkedIn ⚡: @chukwuebuka_nwokporo

GitHub 🚀: @ebukvick

Hashnode 📗: Nwokporo Chukwuebuka

16
Subscribe to my newsletter

Read articles from Nwokporo Chukwuebuka directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nwokporo Chukwuebuka
Nwokporo Chukwuebuka

I'm a developer, who is keen about building solutions in the web. I love to read and write articles.