DEV Community

Cover image for Managing Multiple GitHub Accounts in VS Code: A Comprehensive Guide
FARHAN KHAN
FARHAN KHAN

Posted on

Managing Multiple GitHub Accounts in VS Code: A Comprehensive Guide

Now a days in this fast-paced development environment, it's common for developers to have multiple GitHub accounts. Whether you're working with different organizations, contributing to open-source projects, or maintaining personal repositories, having the ability to switch between accounts within a single instance of Visual Studio Code (VS Code) is invaluable. This guide will walk you through the process of setting up and managing multiple GitHub accounts in VS Code, ensuring you avoid common pitfalls and maintain a streamlined workflow.

Step 1: Setting Up SSH Keys

To securely manage multiple GitHub accounts, you'll need to generate separate SSH keys for each account. This ensures that each account is uniquely identified and authenticated.

Generating SSH Keys:

Open your terminal and use the following command to generate a new SSH key. Replace your_email@example.com with the email associated with your GitHub account:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

You'll be prompted to save the key. For clarity, save it with a distinctive name like key_work for your work account and key_personal for your personal account.

Adding SSH Keys to GitHub:

Next, add the generated SSH key to your GitHub account. Copy the public key using:

cat ~/.ssh/id_rsa_work.pub
Enter fullscreen mode Exit fullscreen mode

Then, navigate to GitHub, go to Settings -> SSH and GPG keys, and add the new key.

Step 2: Configuring SSH for Multiple Accounts

To handle multiple SSH keys, you need to configure your SSH client. Create or edit the ~/.ssh/config file and add entries for each account:

# Work GitHub account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

# Personal GitHub account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode

This configuration tells your SSH client which key to use when connecting to GitHub.

Step 3: Testing SSH Connections

Verify that your SSH keys are correctly configured by testing the connections:

ssh -T git@github.com-work
ssh -T git@github.com-personal
Enter fullscreen mode Exit fullscreen mode

You should receive a message confirming successful authentication for each account.

Step 4: Configuring Git Remotes in VS Code

Now, you need to set up your Git remotes in your project repositories to use the correct SSH configurations.

Adding Remotes:

In your project directory, add the appropriate remote:

git remote add origin git@github.com-work:username/work-repo.git
Enter fullscreen mode Exit fullscreen mode

Verify the remote setup with:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Managing multiple GitHub accounts within a single instance of Visual Studio Code can greatly enhance your workflow, especially if you're juggling projects for different organizations or balancing professional and personal development work. By setting up distinct SSH keys for each account and configuring your SSH client properly, you can seamlessly switch between accounts without encountering permission errors or other common issues.

The steps outlined in this guide—from generating SSH keys and configuring your SSH client, to adding and managing Git remotes—provide a robust framework for maintaining separate GitHub identities. This not only keeps your projects organized but also ensures a secure and efficient way to handle code repositories.

Top comments (2)

Collapse
 
bhanu1776 profile image
Bhanu Sunka • Edited

Very Informative!

Collapse
 
itsfarhankhan28 profile image
FARHAN KHAN

Thanks.