DEV Community

Cover image for Manage Multiple GitHub Accounts with SSH Keys
kshitij Bhatnagar
kshitij Bhatnagar

Posted on

Manage Multiple GitHub Accounts with SSH Keys

In today's dynamic development environment, it's common to juggle multiple GitHub accounts—whether for work, personal projects, or open-source contributions. However, seamlessly switching between your office and personal GitHub profiles can be challenging if not properly managed. This guide will walk you through the best practices and essential steps to effortlessly handle multiple GitHub accounts, ensuring that your workflows remain smooth and your code stays organized, no matter which account you’re using.

Step-by-Step Guide

1. Generate SSH Keys

Generate separate SSH keys for your office and personal GitHub accounts.

Office Account:

ssh-keygen -t rsa -b 4096 -C "your_office_email@example.com" -f ~/.ssh/id_rsa_office
Enter fullscreen mode Exit fullscreen mode

Personal Account:

ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com" -f ~/.ssh/id_rsa_personal_account
Enter fullscreen mode Exit fullscreen mode

2. Add SSH Keys to GitHub

Office Account:

  1. Copy the public key to your clipboard:
    cat ~/.ssh/id_rsa_office.pub

Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub (office account) -> Settings -> SSH and GPG keys -> New SSH key.
  2. Paste the public key and save.

Personal Account:

  1. Copy the public key to your clipboard:
    cat ~/.ssh/id_rsa_personal_account.pub

Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub (personal account) -> Settings -> SSH and GPG keys -> New SSH key.
  2. Paste the public key and save.

3. Configure SSH Agent

Start the SSH agent and add your keys.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_office
ssh-add ~/.ssh/id_rsa_personal_account
Enter fullscreen mode Exit fullscreen mode

4. Configure ~/.ssh/config

Edit your ~/.ssh/config file to define aliases for your office and personal GitHub accounts.

#### Configuration for office account
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_office
    IdentitiesOnly yes

#### Configuration for personal account
Host github.com-personal-account
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal_account
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

5. Clone Repositories

Clone repositories using the appropriate alias to ensure the correct SSH key is used.

Office Repository:

git clone git@github.com:organization/repository.git

Enter fullscreen mode Exit fullscreen mode

Personal Repository:

git clone git@github.com-personal-account:username/repository.git

Enter fullscreen mode Exit fullscreen mode

6. Verify Remote URLs

Check the remote URLs to ensure they are set correctly.

git remote -v

Enter fullscreen mode Exit fullscreen mode

This should show URLs that match the aliases defined in your ~/.ssh/config.

7. Push and Pull Changes

When pushing or pulling changes, Git will automatically use the correct SSH key based on the repository URL.

Push Changes:

git push origin main

Enter fullscreen mode Exit fullscreen mode

Pull Changes:

git pull origin main

Enter fullscreen mode Exit fullscreen mode

Summary of the steps

  1. Generate SSH Keys: Create separate SSH keys for your office and personal GitHub accounts.
  2. Add SSH Keys to GitHub: Add the public keys to the respective GitHub accounts.
  3. Configure SSH Agent: Start the SSH agent and add the generated keys.
  4. Edit ~/.ssh/config: Define aliases for your office and personal accounts, specifying the correct IdentityFile for each.
  5. Clone Repositories: Use the appropriate alias in the repository URL to ensure the correct key is used.
  6. Verify Remote URLs: Ensure the remote URLs are set correctly to match the aliases.
  7. Push and Pull Changes: Git will use the correct SSH key based on the repository URL.

By following these steps, you can seamlessly manage multiple GitHub accounts with different SSH keys, ensuring the correct key is used for each repository.

The best part is that after configuring your ~/.ssh/config file and carefully cloning repositories using the correct aliases, you won’t need to worry about managing the SSH agent during push and pull operations. Git will automatically select the appropriate SSH key based on the repository's URL, making your workflow seamless and hassle-free.

Top comments (0)