Configure Multiple GitHub Accounts on a Single Computer

Configure Multiple GitHub Accounts on a Single Computer

Learn how to configure and manage multiple GitHub accounts on a single computer using SSH keys and Git configurations.

Alex Rodriguez
2024-01-10
3 min read
Table of Contents

Here are step-by-step instructions for configuring and managing multiple GitHub accounts on a single computer, including SSH key setup and Git configuration:

Step 1: Generate SSH Keys for Each GitHub Account

  1. Open your terminal.

  2. Generate a new SSH key for the first GitHub account:

    SH
    ssh-keygen -t rsa -b 4096 -C "your_email_first_account@example.com"
    

    When prompted, save the key with a unique name, e.g., id_rsa_first_account.

  3. Generate a new SSH key for the second GitHub account:

    SH
    ssh-keygen -t rsa -b 4096 -C "your_email_second_account@example.com"
    

    When prompted, save the key with a unique name, e.g., id_rsa_second_account.

Step 2: Add SSH Keys to the SSH Agent

  1. Start the SSH agent in the background:

    SH
    eval "$(ssh-agent -s)"
    
  2. Add the first SSH key to the SSH agent:

    SH
    ssh-add ~/.ssh/id_rsa_first_account
    
  3. Add the second SSH key to the SSH agent:

    SH
    ssh-add ~/.ssh/id_rsa_second_account
    

Step 3: Add SSH Keys to GitHub Accounts

  1. Copy the SSH key for the first account to your clipboard:

    SH
    pbcopy < ~/.ssh/id_rsa_first_account.pub
    

    If pbcopy is not available, you can use:

    SH
    cat ~/.ssh/id_rsa_first_account.pub
    
  2. Log in to your first GitHub account and navigate to Settings > SSH and GPG keys > New SSH key. Paste the key and save.

  3. Repeat the process for the second account:

    SH
    pbcopy < ~/.ssh/id_rsa_second_account.pub
    

    If pbcopy is not available, you can use:

    SH
    cat ~/.ssh/id_rsa_second_account.pub
    
  4. Log in to your second GitHub account and navigate to Settings > SSH and GPG keys > New SSH key. Paste the key and save.

Step 4: Configure SSH to Manage Multiple Accounts

  1. Edit the SSH config file:

    SH
    nano ~/.ssh/config
    
  2. Add the following configurations:

    SH
    # First GitHub account
    Host github-first
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_first_account
    
    # Second GitHub account
    Host github-second
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_second_account
    

Step 5: Configure Git for Multiple Accounts

  1. Set global Git configuration (if not already set):

    SH
    git config --global user.name "Your Name"
    git config --global user.email "your_email@example.com"
    
  2. For repositories associated with the first account, set the local Git configuration:

    SH
    cd path/to/your/repository
    git config user.name "Your Name for First Account"
    git config user.email "your_email_first_account@example.com"
    
  3. For repositories associated with the second account, set the local Git configuration:

    SH
    cd path/to/your/other/repository
    git config user.name "Your Name for Second Account"
    git config user.email "your_email_second_account@example.com"
    

Step 6: Clone Repositories Using the Correct SSH Host

  1. For the first GitHub account, use:

    SH
    git clone git@github-first:username/repository.git
    
  2. For the second GitHub account, use:

    SH
    git clone git@github-second:username/repository.git
    

Step 7: Push and Pull Using the Correct SSH Host

Git will automatically use the correct SSH key based on the configuration in the ~/.ssh/config file.

That's it! You have now configured and can manage multiple GitHub accounts on a single computer.

Share this article:
42 likes
Alex Rodriguez

Alex Rodriguez

JavaScript Instructor

JavaScript expert and former bootcamp instructor. Specializes in making complex concepts simple and engaging for new developers.

Related Articles

Hacktoberfest: A Professional's Approach to Open Source Contribution
#Hacktoberfest
#open source

Hacktoberfest: A Professional's Approach to Open Source Contribution

A seasoned engineer's perspective on Hacktoberfest, focusing on meaningful contributions and professional growth.

Read More

Never Miss an Update

Get the latest tutorials, tips, and insights delivered straight to your inbox.

No spam, unsubscribe at any time.