Byte Ebi's Logo

Byte Ebi 🍀

A Bit everyday A Byte every week

Setting up multiple GitHub accounts on one computer

Configuring multiple GitHub accounts with SSH connections

Ray

Due to my remote work setup, I needed to configure a company GitHub account on my personal computer.
However, GitHub doesn’t allow the same SSH key to be used across multiple accounts, which means I can’t assign my personal SSH key to the company’s GitHub account.

Goal: Set up two SSH keys on one computer, and automatically use the correct one when executing Git commands.

[For reference]: Generating a new SSH key and adding it to the ssh-agent

1. Generate a New SSH Key

ssh-keygen -t rsa -C '{[email protected]}' -f ~/.ssh/id_rsa_company

The SSH key will be saved to:

  • Mac: /Users/username/.ssh
  • Windows C:\Users\username\.ssh

Next, copy the contents of the generated id_rsa.pub.

2. Add the SSH Key to Your Company GitHub Account

Navigate to the SSH keys management page in your company account and click New SSH key.

add ssh key

Give it a name you can easily recognize and paste the id_rsa.pub content you copied earlier.

3. SSH Config Setup

3-1. Clear All SSH Keys

This command will clear the configuration, but it won’t delete the SSH key files.

ssh-add -D

After several attempts, my keys got pretty messy, so I decided to clear everything before proceeding.
Adding keys back is simple and will be part of the next steps.

3.2. Check Existing SSH Keys

ls .ssh

3.3. Re-add the SSH Keys

We previously generated a new SSH key, id_rsa_company, while the personal key is called id_rsa.

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_company

You can verify they were added successfully:

ssh-add -l

3.4. Modify SSH Config

First, ensure the ~/.ssh/config file exists. If not, create it.

cd ~/.ssh/
touch config
open config

Then, edit the file to look like this:

# Personal GitHub account

Host github.com
    HostName github.com
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

# Work GitHub account

Host github.com-company
    HostName github.com
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa_company
    IdentitiesOnly yes

Here, we’ve set up two SSH keys: one for the personal account and one for the company account.

  • Host: refers to the server (either an IP or domain).
  • IdentityFile: points to the SSH key file.
  • AddKeysToAgent: indicates whether the key should be added to the ssh-agent.
  • UseKeychain: specific to macOS and allows the key to be stored in the keychain.

4. Test the Connection

If successful, you should see welcome messages from the respective accounts.

ssh test

5. Clone a Repository

For your personal account, no changes are needed.
However, for company projects, replace the default github.com with github.com-company in the clone command.

git clone [email protected]:{company/repository.git}

In this example, we’ve replaced github.com with github.com-company so that Git automatically uses the correct SSH key.

Some Git GUI tools might not support multiple SSH keys and could give errors, even attempting to use the wrong key (e.g., connecting to the company account when you meant to use your personal one).

If this happens, you might need to clear all SSH keys again.

A more reliable approach is to continue using your favorite GUI for local Git operations, like commit or viewing branches, but run remote commands (e.g., push, pull) directly in the terminal.

6. Adjust Existing Projects

For existing projects, open the /.git/config file in your project folder, and update the URL under [remote "origin"] to point to the company account’s URL.

[remote "origin"]
	url = [email protected]:company/repository.git
	fetch = +refs/heads/*:refs/remotes/origin/*

If your GUI tool allows editing the remote URL, that’s even easier.

That’s it for this guide! I hadn’t faced this issue for a while since I was using a company-issued computer for development.
Although I had done this setup before, I didn’t document it, thinking it wouldn’t come up often.
However, rare problems like this are exactly why we should document themβ€”to avoid spending extra time figuring it out again!

Recent Posts

Categories

Tags