- Git -

Using Multiple GitHub Accounts on the Same Computer

|
6 min read
...

Recently, I stumbled upon a situation where I had to use my personal device for work. I already had my personal GitHub account set up on my machine, and now I had to use my work GitHub account as well. I had to figure out a way to use both accounts on the same machine without any conflicts. In this blog, I will share the steps to set up multiple GitHub accounts on the same computer using SSH keys.

Let's suppose I have two GitHub accounts:

  1. Personal account: https://github.com/anuzpandey
  2. Work account: https://github.com/anuzpandey.office

Here are the steps to set up multiple GitHub accounts on the same computer:

  • Step 1: Generate SSH keys for both accounts.
  • Step 2: Add the SSH Public keys to the GitHub accounts.
  • Step 3: Create a SSH configuration file and add Host entries.
  • Step 4: Clone and Configure the repositories using different accounts.

Step 1: Generate SSH keys for both accounts

Open the terminal of your choice and navigate to the .ssh directory. If the directory does not exist, you can create it using the following command:

Now, generate SSH keys for both GitHub accounts using the following commands:

  • -t rsa: Specifies the type of key to create (RSA) with a length of 4096 bits.
  • -C : Specifies the comment to include in the key.
  • -f "filename": Specifies the filename for the key pair.

The ssh-keygen command will prompt you to enter a passphrase. You can choose to enter a passphrase or leave it empty. If you have entered a passphrase, you will be prompted to enter it whenever you use the private key, so, make sure to remember it.

After running these commands, you will have a public key (github-personal.pub and github-work.pub) and a private key (github-personal and github-work) for each account.

To manage the SSH keys and passphrase configuration, you can use the ssh-agent and ssh-add commands. You can add the private keys to the ssh-agent using the following commands:

Step 2: Add the SSH Public keys to the GitHub accounts

In this step, we will add our SSH public keys to the GitHub accounts. Open the public key files (github-personal.pub and github-work.pub) in a text editor and copy the contents.

You can also use the cat command to display the contents of the public key files or use the pbcopy command to copy the contents to the clipboard.

Now, navigate to the GitHub website and log in to your GitHub accounts. Follow these steps to add the SSH keys:

  1. Go to Settings -> SSH and GPG keys -> New SSH key.
  2. Paste the public key contents in the key field and add a title to identify the key. Leave the "Key Type" as "Authentication Key".
  3. Click on Add SSH key.

Repeat these steps for both GitHub accounts to add the SSH public keys.

Step 3: Create a SSH configuration file and add Host entries

To easily manage multiple SSH keys, we will create a SSH configuration file and add Host entries for each GitHub account. This file will help us specify which SSH key to use for which GitHub account.

We can setup the aliases for the GitHub accounts in the SSH configuration file. Open the SSH configuration file using a text editor:

Add the following Host entries to the SSH configuration file:

In the above configuration:

  • Host: Specifies the alias for the GitHub account.
  • HostName: Specifies the actual GitHub server hostname.
  • User: Specifies the remote user to log in as.
  • IdentityFile: Specifies the path to the private key file.

Step 4: Clone and Configure the repositories using different accounts

Now that we have set up the SSH keys and configuration, we can clone and interact the repositories using different GitHub accounts.

To clone a repository using the Personal account, you can use the following command:

To clone a repository using the Work account, you can use the following command:

When you interact with the repositories, Git will use the SSH keys specified in the SSH configuration file based on the Host alias. To ensure that our commits are associated with the correct user, we can set the user email and name for each repository. To check the current user email and name, you can use the following command:

If the email and name are not set correctly, you can set them using the following commands:

You can do the same for the Personal account by changing the email and name accordingly.

Step 5: Yes, There is a Step 5.

If you have already cloned repositories using the default GitHub URL, you need to update the remote URL to use the Host alias. You can update the remote URL using the following command:

Above command is for the Work account. You can replace the Host alias with the appropriate alias for the Personal account.

Congratulations! You have successfully set up multiple GitHub accounts on the same computer using SSH keys. You can now work on multiple GitHub accounts without any conflicts. If you have any questions or face any issues, feel free to ask in the comments below.

Keep coding, keep exploring, and keep inspiring. 🐼

Written By Anuz Pandey

Anuz Pandey is a multi-disciplinary designer and developer with a passion for creating visually stunning and user-friendly digital experiences. He has been working in the industry for over 5 years. He is currently working as a freelance designer and developer and is available for hire.

Tags

  • git
  • github
  • ssh