Managing Multiple GitHub Accounts on Your Mac Made Easy

October 17, 2023

configdevopsgitgithublinuxsshssh-keystoolsubuntu
Managing Multiple GitHub Accounts on Your Mac Made Easy

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.

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:

  • Open your terminal and navigate to your .ssh folder using the following command
cd ~/.ssh
  • Now, create SSH keys for each account using the following commands as example:
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

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

Add SSH Public Keys to GitHub

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

  1. Run the command below to read the contents of the public key.
cat ~/.ssh/github-office.pub

Select the output and copy it.

  1. Go to your GitHub account, navigate to Settings > SSH and GPG keys, and choose New SSH Key. Paste the public key and give it a title and save it.

Repeat the same process for your other GitHub accounts.

Create a Config File and Define Host Entries

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

  1. Go to your .ssh directory.
cd ~/.ssh

  1. Open the config file in your preferred text editor (if it doesn't exist, you can create one):
vim config

  1. Add the following lines to the config file, one block for each GitHub account:
# 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

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!

Related Link: About SSH key passphrases