Article cover image

How to Add Multiple GitHub Accounts to your Mac

  • Claudio
    Claudio
    Director of Engineering
  • Leonardo
    Leonardo
    Senior Engineer

Agency life requires you to work with multiple clients, each of them having their own GitHub internal policies and requirements. Probably most will just give you access during the development phase, but others will insist on creating a new GitHub account for you. This is a good practice, but it can be a bit annoying to have to switch between accounts all the time. Here’s how to add multiple GitHub accounts to your Mac.

Step 1: Create a new SSH key for each of your GitHub accounts

The first thing you need to do is to create a new SSH key for each of your GitHub accounts. You can do this by following the instructions on GitHub’s website, or simply by running the following command in your terminal:

ssh-keygen -t ed25519 -C "agency-email@example.com" -f "~/.ssh/agency-github-username"

-C is the comment; in this case we use the email address to identify the GitHub account. Your file is generated and saved in ~/.ssh

Repeat the process for each of your client's GitHub account, so that you have a separate SSH key for each of them.

ssh-keygen -t ed25519 -C "agency-email@example.com" -f "~/.ssh/client-github-username"

Step 2. Add the SSH key to the ssh-agent

Once you have created the SSH key(s), you need to add it to your SSH agent. You can do this by running the following command:

ssh-add --apple-use-keychain ~/.ssh/agency-github-username
ssh-add --apple-use-keychain ~/.ssh/client-a-github-username

--apple-use-keychain is a flag that tells the ssh-agent to use the macOS Keychain to store the SSH key.

Step 3. Add the SSH key to your GitHub account

Now that you have created the SSH key and added it to the ssh-agent, you need to add it to your GitHub account. You can do this by following the instructions on GitHub’s website -- should always be up to date, but at high level follow these simple steps:

  • Copy the SSH key to your clipboard with the following command:
pbcopy < ~/.ssh/client-a-github-username.pub
  • Navigate to https://github.com/settings/keys
  • Click on New SSH key
  • Paste the SSH key into the Key field and add a title for the key. (Keep the Key type as "Authentication Key".)
  • Click Add SSH key

Step 4: Modify your SSH config file

Now that you have added the SSH key to your GitHub account, you need to modify your SSH config file (~/.ssh/config).

Here's an example of what your SSH config file should look like:

Host *
	AddKeysToAgent yes
	UseKeychain yes

# Monogram GitHub
Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_ed25519

# GitHub Client-A
Host github.com-client-a
	HostName github.com
	User git
	IdentityFile ~/.ssh/client-a-github-username

# GitHub Client-B
Host github.com-client-b
	HostName github.com
	User git
	IdentityFile ~/.ssh/client-b-github-username

# GitHub Client-C Enterprise Account
Host github.client-c-enterprise.cloud
	HostName github.client-c-enterprise.cloud
	User git
	IdentityFile ~/.ssh/client-c-github-username

Step 5: Clone your repositories

Now that you have set up your SSH config file, you can clone your repositories. You can do this by running the following command:

git clone git@github.com-client-a:<client-a-org-name>/<repo-name>.git

One thing to note is that you need to use the github.com&lt;-client-a&gt; even though they're both hosted on GitHub. This is because the SSH config file tells the SSH client to use the IdentityFile for github.com-client-a, and the host is an alias for github.com defined in the HostName field.

Your main agency account is set up by default, so you don't need to specify anything after github.com in the host when cloning your agency's or personal repositories.