How to Manage Multiple GitHub Accounts on the Same Machine: A Step-by-Step Guide

Β·

3 min read

Are you a developer juggling multiple GitHub accounts? Keeping track of different accounts can be a hassle, especially when contributing to a variety of open-source projects or working for multiple companies. The good news is that there are easy-to-follow steps that can help you set up and configure multiple GitHub accounts on the same machine.

In this post, we'll walk through the steps required to successfully set up and configure multiple GitHub accounts on one machine. By following these simple steps, you'll be able to keep your work organized, stay connected to the right account, and contribute more easily.

Basic Principles of GitHub Accounts

Before we start, let's review the basic principles of how GitHub operates:

  • Every GitHub account is associated with a unique email address

  • Git relies on a global user configuration to identify the committer's name and email on each commit

  • Git uses SSH keys to authenticate users

Steps for Setting Up Multiple Accounts

1. Generate New SSH Keys

The first step is to generate new SSH keys. This is done so that each account has a unique key associated with it. To create new SSH keys using the ssh-keygen command in terminal, follow these steps:

ssh-keygen -t rsa -C "your-email@example.com" -f ~/.ssh/github-account1
ssh-keygen -t rsa -C "your-email@example.com" -f ~/.ssh/github-account2

Copy

This will create two new SSH keys: ~/.ssh/github-account1 and ~/.ssh/github-account2.

2. Add SSH Keys to GitHub

After generating the new SSH keys, the next step is to add them to each GitHub account. Here's how to do it:

  • Login to the first account and go to "Settings" and then "SSH and GPG keys."

  • Click on "New SSH key" and paste the contents of the ~/.ssh/github-account1.pub key.

Repeat the process for each account, ensuring that you add the corresponding SSH key to the correct account.

3. Create an SSH Config File

To tell Git which SSH key to use for each account, you need to create a ~/.ssh/config file. You can do this by following these steps:

Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-account1

Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-account2

Copy

This configures Git to use the correct SSH key based on the hostname of github.com-account, which means that if you're using the first account, it will use github.com-account1, and if you're using the second account, it will use github.com-account2.

4. Configure Git

The next step is to configure Git to use the correct name and email for each account. Here's how to do it:

Navigate to your home directory and create a new directory .github where you will store your Git configuration files:

cd ~
mkdir .github
cd .github

Copy

Create a new file called config-account1 and add the following content:

[user]
    name = Account1
    email = account1@example.com

Copy

Create another new file called config-account2 and add the following content:

[user]
    name = Account2
    email = account2@example.com

Copy

The config-account1 file will configure Git to use the name and email that correspond to the first account, and the config-account2 file will configure Git to use the second account.

5. Use Git

Now that everything is configured, you can use Git with multiple accounts. To clone a repository, use the SSH URL with the appropriate hostname:

git clone git@github.com-account1:username/repo.git
git clone git@github.com-account2:username/repo.git

Copy

When you push to a remote repository, Git will use the correct SSH key and send the right name and email based on the remote URL.

Conclusion

In conclusion, setting up and managing multiple GitHub accounts on the same machine is not as daunting as it may initially seem. By following these easy-to-follow steps and keeping your work organized, you'll be able to work on multiple accounts effortlessly.

We hope this guide has been helpful to you. Happy coding!

Did you find this article valuable?

Support Rishav Trivedi by becoming a sponsor. Any amount is appreciated!

Β