DEV Community

Cover image for Using Multiple GitHub Accounts on One Machine with SSH
Aseer
Aseer

Posted on

Using Multiple GitHub Accounts on One Machine with SSH

Overview

Have you ever wanted to use more than one GitHub account on your laptop? Maybe you want to use a work account and a personal account. Or maybe you want to use accounts from different remotes such as GitHub, GitLab, and BitBucket. It's easy to set up on any operating system.

We will be using the SSH method to authenticate your remote account with Git. If you haven't used SSH before, this is a great opportunity to learn the basics because it is a very useful tool in many other fields as well.

This article is written using this video as a reference. Save it and watch it every time you get confused, that's what I did.


Adding the First Account

  1. Open your terminal if you're on Mac or Linux. If you're on Windows, open Git Bash.

  2. Navigate to ~/.ssh.

    $ cd ~/.ssh
    
  3. Generate SSH keys by running the keygen command. Give it an appropriate name, it's best to use the username of the GitHub account.

    $ ssh-keygen -f username1
    
  4. Skip all the options by pressing enter. Run ls to make sure two new files have been created. A public key and a private key.

  5. Run the following command to see the public key.

    $ cat username1.pub
    
  6. Open GitHub and make sure you are signed in to the account for the username you just used. If you are not, then sign in to the correct account.

  7. Navigate to Settings » SSH and GPG keys and click on New SSH key.

  8. The title is supposed to signify the device you are using. It is only for your reference.

    • If you are on something like a Dell laptop, then enter "Dell laptop". Whatever helps you identify this device.
    • Open the terminal and copy the public key. Back in GitHub, paste the public key and click Add SSH key.
  9. In the terminal, create a config file and open it in your editor.

    $ touch config
    $ code config
    
  10. Write something like this in the config file. Replace username1 with the username you used. And replace github.com-user1 with any other abbreviation you like, such as github.com-personal but let it be short.

    # Account for username1
    Host github.com-user1
        HostName github.com
        IdentityFile ~/.ssh/username1
    
  11. The setup is complete, let's test it. Open a private repo on your GitHub account. Click on the green Clone button and select SSH. Copy the string it gives you.

    Image description

  12. Open a new terminal in the Desktop folder. Enter the following command.

    $ git clone git@github.com-user1:<full name of the repo>
    

    Image description

💡 Remember to add -user1 right after the github.com. In the above image, I have used github.com-ele because that's what is in my config file.

🎉You did it! Now this local repo is directly connected with the GitHub repo. With all the branch information and remotes and everything. You can continue using it like you've always used GitHub.


Adding the Second Account

Follow the same steps above with the following differences.

  1. Open the browser and sign out of the previous GitHub account. Sign in with the second account that you want to use, say your work account.

  2. In the terminal, make sure you are in the ~/.ssh directory. There, generate the new SSH keys.

    $ cd ~/.ssh
    $ ssh-keygen -f username2
    
  3. Run ls to make sure there are two new files. There should be at least 5 files in total. Open the config file and update the content as shown below.

    $ ls
    $ code config
    

    And the config file looks like...

    # Account for username1
    Host github.com-user1
        HostName github.com
        IdentityFile ~/.ssh/username1
    
    # Account for username2
    Host github.com-user2
        HostName github.com
        IdentityFile ~/.ssh/username2
    

    As earlier, replace github.com-user2 with anything you like, such as github.com-work.

  4. Run cat username2.pub to see the new public key. In GitHub, create the SSH key just as we did before. You can use the same title if you'd like since this is only for your reference. Paste the key.

  5. Now test it with a private repo on this account, exactly like we tested the first account. Remember to use github.com-user2 in the clone command.

🎉🎉You're using two GitHub accounts on the same machine! You can work on private repos of both accounts without feeling any difference in the way you use Git.


The End

Welp, that's the end of it.

Are there more tips & tricks to use Git productively that beginners should learn? Share them in the comments.

If you like this article then consider following me on Hashnode.


Top comments (0)