Managing Multiple GitHub Accounts on Your Mac Made Easy

Sujit Patel
2 min readOct 17, 2023

--

If you find yourself in a situation with multiple GitHub accounts, one for your personal projects and another for your company’s work, you might encounter issues when cloning repositories, such as using the wrong account’s credentials. This is a common challenge for developers. Here’s a straightforward guide to solve this problem in five simple steps.

Step 1: Generate SSH Keys for Each GitHub Account

First things first, you need SSH keys for each GitHub account to securely communicate with them. Follow these steps:

cd ~/.ssh
ssh-keygen -t rsa -C "your_office_email@gmail.com" -f "github-office" 
ssh-keygen -t rsa -C "your_personal_email@gmail.com" -f "github-personal"
  • The -C option helps identify your key.
  • The -f option specifies the file name for your SSH key.

When you run these commands, you’ll be asked to enter a passphrase, which you can either provide (remember it because you can’t recover it later) or leave empty. These commands will create two files for each account, one is public key with a .pub extension and a private key without an extension in your .ssh folder.

For example:

  • Office Account keys: github-office and github-office.pub
  • Personal Account keys: github-personal and github-personal.pub

Step 2: Add SSH Keys to the SSH Agent

Now that you have your keys, add them to the SSH Agent so they can be used.

ssh-add -K ~/.ssh/github-office
ssh-add -K ~/.ssh/github-personal

Step 3: Add SSH Public Keys to GitHub

Next, add your public keys to your respective GitHub accounts. Follow these steps:

cat ~/.ssh/github-office.pub

Select the output and copy it.

Repeat the same process for your other GitHub accounts.

Step 4: Create a Config File and Define Host Entries

Now, create a ~/.ssh/config file to manage your accounts effectively. Follow these steps:

cd ~/.ssh
vim config
# Office account
Host github.com-office
HostName github.com
User git
IdentityFile ~/.ssh/github-office
# Personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-personal

Also Read: How to create EKS cluster using AWS CLI

Step 5: Clone GitHub Repositories Using Different Accounts

You’re all set up, and it’s time to put it to the test! Let’s say you want to clone a repository using your personal account.

  1. Go to the directory where you want to clone the repository.
  2. To clone it, use the following command, replacing the placeholders with your own information:
git clone git@github.com-personal:{repo-url}.git

Examples:

For Perosnal:

git clone git@github.com-personal:psujit775/terraform-templates.git

For Office:

git clone git@github.com-office:psujit775/terraform-templates.git

By following these steps, you’ll save time and avoid potential mistakes when managing multiple GitHub accounts, making your development workflow smoother and more efficient. Whether it’s for personal projects or company work, you can now seamlessly switch between accounts and repositories with confidence. Happy coding!

Originally published at https://sujitpatel.in on October 17, 2023.

--

--

Sujit Patel
Sujit Patel

Written by Sujit Patel

DevOps Engineer, Linux lover, Technology and Automation enthusiast. A strong believer in continuous learning.

No responses yet