How to set up more than one Github account | neoco

/how-to-manage-two-github-accounts

How to set up more than one Github account

Joan Antoni Morey

Joan Antoni Morey

3 min

05/03/2023

Have you had to use a second Github account to clone a repository and have been unable to do so? As you may have noticed, it's not as simple as doing a git clone of the new repository and you're done. In this case, you will need to do a small configuration of the "config" file in the ".ssh" folder.

If you only use one Github account, you probably never have to modify that file because you will use the default configuration. The problem arises when you need to use another Github account and perform any action with that account on a repository (clone, pull, push...), either because a client has provided you with another account for their project, or because you are a student and your school or university has provided you with student credentials on Github, or simply because you have just created another Github account for whatever reason.

What do we do in case this happens? First, we need to generate our SSH keys, upload them to Github, and add them to our ".ssh" folder.

"config" File

Assuming you already have your SSH keys for both Github accounts, let's configure them! In the following example, let's suppose you have a personal and a work account that you need to use on the same machine. Here are the steps to follow in order to use both:

  1. Open the terminal and access the ".ssh" folder in your user directory.
  2. Create a configuration file named "config" if it doesn't already exist.
  3. Open the "config" file with your text editor.
  4. Add the following information to your "config" file:
# Personal Github account
Host github.com
HostName github.com
User your_personal_github_username
IdentityFile ~/.ssh/id_rsa_personal

# Work Github account
Host github-work
HostName github.com
User your_work_github_username
IdentityFile ~/.ssh/id_rsa_work
  1. Save the "config" file.
  2. To clone or interact with Github repositories from your personal account, use the git command with the following syntax:
git clone git@github.com:personal_username/repository_name.git
  1. To clone or interact with Github repositories from your work account, use the git command with the following syntax:
git clone git@github-work:work_username/repository_name.git

Remember to use the "github-work" address for the work account instead of "github.com".

Problem solved

With these steps, you can now use both of your Github accounts on the same machine. Configuring two Github accounts may seem a bit complicated at first, but with the right steps and correct information, it's easy to do. Don't get discouraged if it takes you a little time to understand the process. Remember... practice makes perfection.