How to Use Two GitHub Accounts in VS Code: Switching Between Personal & Company Accounts

TutorialPedia Team

Date Updated

As developers, we often juggle multiple GitHub accounts: one for personal projects (e.g., open-source contributions, side hustles) and another for work (company repositories, team collaborations). However, using two accounts simultaneously in VS Code can lead to frustrating issues—like accidental commits with the wrong email, authentication errors, or permission denials.

The root cause? By default, Git and VS Code use a single global configuration, making it hard to separate identities. This guide will walk you through a step-by-step solution to seamlessly manage two GitHub accounts in VS Code, using SSH keys for secure authentication and per-repository Git configurations to keep your personal and work identities distinct.

Table of Contents#

  1. Prerequisites
  2. Step 1: Setting Up SSH Keys for Both Accounts
  3. Step 2: Configuring Git Identities
  4. Step 3: Cloning Repositories with the Correct Account
  5. Step 4: Switching Between Accounts in VS Code
  6. Troubleshooting Common Issues
  7. Advanced Tips: Automate Identity Switching
  8. Conclusion
  9. References

Prerequisites#

Before starting, ensure you have:

  • Git installed (v2.28+ recommended for conditional config support).
  • VS Code installed (with the official Git extension enabled, which is pre-installed by default).
  • Two GitHub accounts: a personal account (e.g., john-doe-personal) and a company account (e.g., john-doe-company).
  • Basic familiarity with the terminal (Command Prompt, PowerShell, or Git Bash on Windows; Terminal on macOS/Linux).

Step 1: Setting Up SSH Keys for Both Accounts#

SSH keys are the most secure way to authenticate with GitHub, and they let you use multiple accounts without entering passwords repeatedly. We’ll create separate SSH keys for your personal and company accounts.

Generate Unique SSH Keys#

By default, Git uses id_rsa as the default SSH key filename. To avoid conflicts, we’ll generate unique filenames for each account:

  1. Open your terminal.

  2. Generate a key for your personal account:

    ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_rsa_personal  
    • -t ed25519: Uses the Ed25519 algorithm (more secure than RSA).
    • -C "email": Adds a comment (your email) to identify the key.
    • -f ~/.ssh/id_rsa_personal: Saves the key to ~/.ssh/id_rsa_personal (instead of the default id_rsa).
  3. When prompted for a passphrase, enter a strong one (optional but recommended for security).

  4. Repeat for your company account:

    ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_rsa_company  

Add Keys to the SSH Agent#

The SSH agent manages your keys, so you don’t have to re-enter passphrases every time.

  1. Start the SSH agent (if it’s not running):

    # For macOS/Linux  
    eval "$(ssh-agent -s)"  
     
    # For Windows (Git Bash)  
    eval $(ssh-agent -s)  
     
    # For Windows PowerShell  
    Start-Service ssh-agent  
  2. Add your personal key to the agent:

    ssh-add ~/.ssh/id_rsa_personal  
  3. Add your company key:

    ssh-add ~/.ssh/id_rsa_company  

    Note: On macOS, keys may be unloaded after a restart. To persist them, add ssh-add -K ~/.ssh/id_rsa_personal (and -company) to your ~/.zshrc or ~/.bash_profile.

Create an SSH Config File#

An SSH config file tells your system which key to use for which GitHub connection.

  1. Create/Edit the config file:

    # macOS/Linux  
    nano ~/.ssh/config  
     
    # Windows (Git Bash)  
    nano ~/.ssh/config  
     
    # Windows PowerShell  
    notepad ~/.ssh/config  
  2. Add the following content (replace your_personal_email and your_company_email with your actual emails):

    # Personal GitHub Account  
    Host github.com-personal  
      HostName github.com  
      User git  
      IdentityFile ~/.ssh/id_rsa_personal  
      IdentitiesOnly yes  
     
    # Company GitHub Account  
    Host github.com-company  
      HostName github.com  
      User git  
      IdentityFile ~/.ssh/id_rsa_company  
      IdentitiesOnly yes  
    • Host github.com-personal: Creates an alias (github.com-personal) to use with your personal key.
    • IdentityFile: Specifies the path to your personal/company key.
    • IdentitiesOnly yes: Ensures only the specified key is used (avoids conflicts).
  3. Save and close the file (in nano, press Ctrl+O, Enter, then Ctrl+X).

Add Public Keys to GitHub#

Next, add the public keys (.pub files) to your GitHub accounts.

For Your Personal Account:#

  1. Copy the public key to your clipboard:

    # macOS  
    pbcopy < ~/.ssh/id_rsa_personal.pub  
     
    # Linux  
    xclip -sel clip < ~/.ssh/id_rsa_personal.pub  
     
    # Windows (Git Bash)  
    cat ~/.ssh/id_rsa_personal.pub | clip  
  2. Go to GitHub Personal Settings > SSH and GPG keys.

  3. Click New SSH key, add a title (e.g., “Personal Laptop”), and paste the key.

  4. Click Add SSH key.

For Your Company Account:#

Repeat the steps above with id_rsa_company.pub and your company GitHub account.

Verify SSH Connections#

Test that each key works:

# Test personal account  
ssh -T [email protected]  
# Expected output: "Hi your_personal_username! You've successfully authenticated..."  
 
# Test company account  
ssh -T [email protected]  
# Expected output: "Hi your_company_username! You've successfully authenticated..."  

Step 2: Configuring Git Identities#

Git uses user.name and user.email to label commits. We’ll set per-repository identities to ensure commits use the correct name/email for personal vs. company repos.

Global vs. Per-Repository Configuration#

  • Global config: Applies to all repos by default (we’ll avoid relying on this).
  • Per-repo config: Overrides the global config for a specific repo (we’ll use this).

Set Up Personal and Company Identities#

  1. First, clear any global Git identity (optional but recommended to avoid accidental use):

    git config --global --unset user.name  
    git config --global --unset user.email  
  2. For each personal repo you work on, set the personal identity:

    # Navigate to your personal repo  
    cd ~/projects/personal-repo  
     
    # Set personal name/email  
    git config user.name "John Doe (Personal)"  
    git config user.email "[email protected]"  
  3. For each company repo, set the company identity:

    # Navigate to your company repo  
    cd ~/projects/company-repo  
     
    # Set company name/email  
    git config user.name "John Doe (Company)"  
    git config user.email "[email protected]"  

    Verify the config with git config --list (look for user.name and user.email).

Step 3: Cloning Repositories with the Correct Account#

Now that SSH is configured, clone repos using the SSH aliases we set up (github.com-personal or github.com-company).

Cloning Personal Repos#

To clone a personal repo (e.g., my-blog):

git clone [email protected]:your_personal_username/my-blog.git  
  • The github.com-personal alias tells SSH to use your personal key.

Cloning Company Repos#

To clone a company repo (e.g., company-app):

git clone [email protected]:your_company_org/company-app.git  
  • The github.com-company alias uses your company key.

Step 4: Switching Between Accounts in VS Code#

VS Code will automatically use the correct SSH key and Git identity when you open a repo—no manual switching required!

Working with Personal Repos#

  1. Open VS Code.
  2. Open your personal repo: File > Open Folder > ~/projects/my-blog.
  3. Make a test change (e.g., edit README.md).
  4. Commit the change:
    • Open the Source Control tab (Ctrl+Shift+G).
    • Enter a commit message (e.g., “Update README”).
    • Click Commit.

VS Code will use the user.name and user.email set in the repo’s local config.

Working with Company Repos#

Repeat the process with a company repo:

  1. Open the company repo in VS Code: File > Open Folder > ~/projects/company-app.
  2. Make a change and commit—it will use your company identity.

Verifying the Active Account#

To confirm you’re using the right account:

  • Check the commit author in the Source Control tab (under “Changes”).
  • Run git log --pretty=format:"%an <%ae>" -1 in the VS Code terminal to see the last commit’s author.

Troubleshooting Common Issues#

SSH Connection Errors#

  • “Permission denied (publickey)”: Ensure the SSH key is added to the agent (ssh-add -l to list keys). If not, re-run ssh-add ~/.ssh/id_rsa_personal.
  • “Bad configuration option: IdentitiesOnly”: Update your SSH client (old versions don’t support IdentitiesOnly).

Incorrect Commit Author#

  • Check the repo’s local Git config: git config user.email. If wrong, re-run git config user.email "correct_email".
  • Ensure you didn’t set a global user.email (run git config --global user.email to check).

SSH Agent Not Running#

  • On Windows: Start the agent with Start-Service ssh-agent (PowerShell) or eval $(ssh-agent -s) (Git Bash).
  • On macOS/Linux: Add eval "$(ssh-agent -s)" to your ~/.bashrc or ~/.zshrc to auto-start the agent.

Advanced Tips: Automate Identity Switching#

Manually setting user.name/user.email for every repo is tedious. Use Git conditional includes to auto-apply identities based on repo location.

Step 1: Organize Repos by Account#

Store personal and company repos in separate folders:

~/projects/  
  personal/  # All personal repos here  
  company/   # All company repos here  

Step 2: Create Git Config Files#

  1. Create a personal config file:

    nano ~/.gitconfig-personal  

    Add:

    [user]  
      name = John Doe (Personal)  
      email = [email protected]  
  2. Create a company config file:

    nano ~/.gitconfig-company  

    Add:

    [user]  
      name = John Doe (Company)  
      email = [email protected]  

Step 3: Update Global Git Config#

Edit your global .gitconfig to conditionally include these files:

nano ~/.gitconfig  

Add:

[includeIf "gitdir:~/projects/personal/"]  
  path = ~/.gitconfig-personal  
 
[includeIf "gitdir:~/projects/company/"]  
  path = ~/.gitconfig-company  

Now, any repo in ~/projects/personal/ will auto-use your personal identity, and ~/projects/company/ will use your company identity!

Conclusion#

By following these steps, you can seamlessly use two GitHub accounts in VS Code:

  • SSH keys handle authentication for each account.
  • Per-repo Git config (or conditional includes) ensures commits use the correct identity.
  • No more authentication errors or accidental personal commits in company repos!

References#