Using Git With Multiple GitHub Accounts

By William Estoque

Last updated Dec 10, 2022

Using git with multiple GitHub accounts present many problems for the user because one simply cannot use the same SSH public key for different accounts.

You can use HTTP but then you have to input your GitHub username and password everytime you have to push/pull.

We have created the guide below so you can use GitHub using multiple accounts with the easiest setup.

The steps to use GitHub using multiple accounts:

  1. Create your new GitHub user

    Here at Narra Labs, we follow a naming pattern for naming a new GitHub user. If your personal GitHub username for example is westoque, and you are working for a company named narralabs, then your new company GitHub account username would be westoque-narralabs.

  2. Generate a new ssh key pair for your new user using ssh-keygen and follow the instructions. Make sure to name the SSH key using the same pattern as your newly created GitHub username.

     westoque@milan$ ssh-keygen
     Generating public/private rsa key pair.
     Enter file in which to save the key (/Users/westoque/.ssh/id_rsa): westoque-narralabs
     Enter passphrase (empty for no passphrase):
     Enter same passphrase again:
     Your identification has been saved in westoque-narralabs
     Your public key has been saved in westoque-narralabs.pub
     The key fingerprint is:
     SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX westoque@milan.lan
     The key's randomart image is:
     +---[RSA 3072]----+
     |     X XXXXXX    |
     |    X X XXXX     |
     |     X XXXX      |
     |    X X XX       |
     |     X XXX       |
     |      X X X      |
     |     X           |
     |                 |
     |                 |
     +----[SHA256]-----+
     

    Two files will be generated by the command above:

    1. The private key: westoque-narralabs
    2. The public key: westoque-narralabs.pub
  3. Move the new SSH key files to your ~/.ssh directory
    mv westoque-narralabs westoque-narralabs.pub ~/.ssh
  4. Modify your SSH config by modifying ~/.ssh/config:

    Add the line below to tell the SSH program that you have a new host using the following configurations.

    Host github-westoque-narralabs
        User git
        HostName github.com
        IdentityFile ~/.ssh/id_rsa_westoque_narralabs
  5. Clone the project using your new host:

    The original GitHub SSH clone link would look like below:

    git@github.com:narralabs/brackets.git

    You then have to modify it using your new host:

    github-westoque-narralabs:narralabs/brackets.git

    Then clone the project:

    git clone github-westoque-narralabs:brackets.git
  6. You can now push/pull like normal for this specific repository.

You can repeat the steps above for another GitHub account.