How to Use Multiple GitHub Accounts on MacOS

Photo by Yancy Min on Unsplash

How to Use Multiple GitHub Accounts on MacOS

Introduction

GitHub is a powerful platform for collaboration and version control, widely used by developers worldwide. However, managing multiple GitHub accounts on the same machine can be tricky. In this guide, we'll walk you through the steps to set up and use two GitHub accounts on your Mac, allowing you to seamlessly switch between personal and work projects.

Step 1: Generate Separate SSH Keys

SSH keys are used for secure authentication when interacting with GitHub repositories. To use multiple GitHub accounts, you'll need to generate separate SSH keys for each account.

  1. Open Terminal on your Mac.

  2. Run the following command to generate an SSH key for the first account, replacing "your_email@example.com" with your actual email address:

     ssh-keygen -t ed25519 -C "your_email@example.com"
    
     #For legacy systems use
     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  3. When prompted, save the key with a unique filename, such as "id_rsa_your_name".

Repeat the process for the second account, using a different filename like "id_rsa_company_name".

Step 2: Configure SSH Config File

Next, you'll need to configure your SSH config file to use the correct SSH key for each GitHub account.

  1. Open or create the SSH config file by running:

     nano ~/.ssh/config
    
  2. Add the following configuration for each GitHub account:

     # Personal GitHub account
     Host github.com-personal
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_john
    
     # Work GitHub account
     Host github.com-work
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_google
    

Step 3: Add Public Keys to GitHub Accounts

Now, you'll need to add the public keys to the corresponding GitHub accounts.

  1. Log in to your personal GitHub account and go to the "SSH and GPG keys" section of your Settings page.

  2. Click "New SSH key" and paste the contents of "~/.ssh/id_rsa_john.pub".

     pbcopy < ~/.ssh/id_rsa_john.pub
    
  3. Repeat this process for your work GitHub account, using the "id_rsa_google.pub" key.

Step 4: Test Connections

To ensure everything is set up correctly, test the connections for both accounts.

  1. Run the following commands in Terminal:

     ssh -T git@github.com-personal
     ssh -T git@github.com-work
    

Step 5: Working with Repositories

When cloning or working with repositories, use the appropriate hostname:

git clone git@github.com-personal:your-username/repo.git
git clone git@github.com-work:your-org-reponame/repo.git

Step 6: Set Git User Configuration

Lastly, set the Git user.email and user.name for each repository:

git config user.email "your_personal_email@example.com"
git config user.name "Your Personal Name"
git config user.email "your_work_email@example.com"
git config user.name "Your Work Name"

Conclusion

By following these steps, you can seamlessly use two GitHub accounts on your Mac, switching between them as needed for your personal and work projects. Properly configured SSH keys and Git configurations ensure smooth interactions with GitHub repositories, enhancing your productivity and organization.

Thank you for joining me on this technical journey. Your support and interest mean the world to me. I look forward to sharing more insightful content with you in the future. Until then, stay curious and keep exploring!