Use two GitHub accounts on the same computer

Many developers have multiple GitHub accounts. One for personal projects, and another one for their work or business. There is a way to access multiple accounts simultaneously from the same computer. Only one account at a time can be configured using the HTTP connection, but we can configure the rest of the accounts using SSH.

There are many great articles on this topic, but most of them only provide partial instructions on how to set up multiple GitHub accounts on the same computer. In this post, I will document all the necessary steps to set up two accounts.

Usually, you don’t have much control over what GitHub connectivity is used for the company account, it can be dictated by the customer or your workplace, but you can always control what to use for your personal account. With SSH you can specify all aspects of your personal account, so you can leave the default SSH or HTTP connection for the business or company account.

In this post, we will set up two accounts with SSH connection. Even if the business account currently uses HTTP, it makes sense to set up SSH for that account too, in case they will switch to SSH at a later time.

Create two keys

    1. Open a terminal window and start the authentication agent to avoid the “Could not open a connection to your authentication agent” error later
       eval `ssh-agent -s`
    2. Create the personal SSH key
      ssh-keygen -t rsa -C "MY_PERSONAL_EMAIL@ADDRESS"

      when asked for the file name by the “Enter file in which to save the key“prompt, save the key as id_rsa_pers

    3. Add the personal key to the authentication agent
      ssh-add ~/.ssh/id_rsa_pers
    4. Create the business SSH key
      ssh-keygen -t rsa -C "MY_COMPANY_EMAIL@ADDRESS"

      when asked for the file name by the “Enter file in which to save the key“prompt, save the key as id_rsa_COMPANY_NAME

    5. Add the business key to the authentication agent
      ssh-add ~/.ssh/id_rsa_COMPANY_NAME

Configure SSH to use the two new keys

  1. Create the SSH config file
    touch ~/.ssh/config
  2. Open the ~/.ssh/config file with a text editor
  3. Add the following lines
    #Personal GitHub
    Host github-pers
      HostName github.com
      User MY_PERSONAL_GITHUB_USERNAME
      IdentityFile ~/.ssh/id_rsa_pers
    
    #Company GitHub
    Host github.com
      HostName github.com
      User MY_COMPANY_GITHUB_USERNAME
      IdentityFile ~/.ssh/id_rsa_COMPANY_NAME
    

Upload the keys to your GitHub accounts

  1. Log into the GitHub website with your personal credentials
  2. In the upper right corner click the down arrow next to your icon, and select Settings
  3. On the left side select SSH and GPG keys
  4. Click the New SSH key button
  5. Type a name for the key,  paste the contents of the ~/.ssh/id_rsa_pers.pub public key file into the text area, and click the Add SSH key button
  6. Log into Github with your company credentials
  7. Upload the id_rsa_COMPANY_NAME.pub public key file as explained above.

Create a personal repository

  1. Create a new folder, personal-test, and add a file to it on your computer
  2. Initialize the Git repository in the folder
    git init
    git add .
    git commit -m "Initial commit"
  3. Log into your personal GitHub account
  4. Create a new repository, and name it personal-test
  5. On the confirmation page click the SSH button
  6. Copy the …or push an existing… code to a text editor
  7. Edit the origin address to match the “Host” of the personal section in the ~/.ssh/config file, and execute the commands
    git remote add origin git@github-pers:MY_PERSONAL_GITHUB_USERNAME/personal-test.git
    git push -u origin master

Company repositories

For your company account use the default HTTP or SSH GitHub repository address without any modifications.

Leave a Reply