How to Manage Multiple GitHub Accounts on the Same Computer

How to Manage Multiple GitHub Accounts on the Same Computer

Posted By

kamlesh paul

on

Dec 10, 2024

Table of contents

Overview:

Managing multiple GitHub accounts on the same computer can be done by creating separate SSH keys for each account and setting up a configuration file to handle them. This setup prevents conflicts between accounts and allows you to easily clone repositories and push changes without confusion.

How it works:

  1. You’ll generate a separate SSH key for each GitHub account.
  2. Configure the ~/.ssh/config file to map each key to an alias for GitHub, making it clear which key corresponds to which account.
  3. You can then clone, push, and pull from repositories using the correct account by referencing the custom aliases.

Steps to Manage Multiple GitHub Accounts:

Generate SSH Keys for Each GitHub Account

  • To get started, create an SSH key for each account. First, go to your .ssh directory:
cd ~/.ssh

Now generate the SSH keys for your GitHub accounts. In this example, we’ll create SSH keys for two accounts—one for a client account and one for a side project:

ssh-keygen -t rsa -C "client_email@example.com" -f "github-client"
ssh-keygen -t rsa -C "side_project_email@example.com" -f "github-sideproject"

This command creates a private key and a corresponding public key.

mulitple-ssh-key.webp

Add SSH Keys to the SSH Agent

After generating the keys, add them to your SSH agent:

ssh-add ~/.ssh/github-client
ssh-add ~/.ssh/github-sideproject

This step ensures that the keys are available for authentication.

Add SSH Public Keys to GitHub

Next, you’ll need to add the public keys to your GitHub accounts. To do this, first copy the public keys:

pbcopy < ~/.ssh/github-client.pub
 
pbcopy < ~/.ssh/github-sideproject.pub

Then, log in to each GitHub account and go to:

  • Settings > SSH and GPG keys > New SSH Key

Paste the corresponding public key and give it a descriptive title (e.g., “Client Account” or “Side Project”).

Configure the SSH Config File with Aliases

To avoid confusion between accounts, you’ll need to create aliases in your SSH config file. If you don’t already have a config file, create one:

touch ~/.ssh/config 
open ~/.ssh/config

Now, add the following lines to the config file to set up aliases for each account:

# Client account
Host github.com-client
     HostName github.com
     User git
     IdentityFile ~/.ssh/github-client
 
# Side project account
Host github.com-sideproject
     HostName github.com
     User git
     IdentityFile ~/.ssh/github-sideproject

By creating aliases like github.com-client and github.com-sideproject, you can easily differentiate between accounts when interacting with GitHub.

Clone and Manage Repositories Using the Correct Account

When you want to clone a repository, use the alias you created in the SSH config. For example, to clone a repo using your client account:

git clone git@github.com-client:username/client-repo.git

And to clone a repository for your side project:

git clone git@github.com-sideproject:username/sideproject-repo.git

This ensures that the correct SSH key is used for each account.

Adding a Remote to an Existing Repository

If you already have a Git repository set up locally and need to add a remote for a specific GitHub account, you can add the remote manually using the correct alias from your SSH config. Here’s how to do it:

  1. Navigate to your project’s directory:
cd path/to/your/repo
  1. Add the correct remote for the account you want to associate with this repository. For example, to add a remote for your client account, use:
git remote add origin git@github.com-client:username/client-repo.git

For your side project account, use:

git remote add origin git@github.com-sideproject:username/sideproject-repo.git
  1. Verify the remote has been added:
git remote -v

You should see the remote URL associated with the correct account.

Now, whenever you push or pull changes from this repository, Git will use the correct SSH key based on the alias you provided. If needed, you can also switch between remotes by using git remote set-url to update the remote URL.

For example, if you need to change the remote to another account later:

git remote set-url origin git@github.com-sideproject:username/new-repo.git

Conclusion

Setting up multiple GitHub accounts on the same computer is easy by following these steps. Using SSH keys and configuring the ~/.ssh/config file allows you to manage client work, side projects, and personal repositories seamlessly without any confusion. This method can be scaled to handle as many GitHub accounts as needed.

Share this article

36 views