How to Use Two GitHub Accounts in VS Code: Switching Between Personal & Company Accounts
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#
- Prerequisites
- Step 1: Setting Up SSH Keys for Both Accounts
- Step 2: Configuring Git Identities
- Step 3: Cloning Repositories with the Correct Account
- Step 4: Switching Between Accounts in VS Code
- Troubleshooting Common Issues
- Advanced Tips: Automate Identity Switching
- Conclusion
- 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:
-
Open your terminal.
-
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 defaultid_rsa).
-
When prompted for a passphrase, enter a strong one (optional but recommended for security).
-
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.
-
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 -
Add your personal key to the agent:
ssh-add ~/.ssh/id_rsa_personal -
Add your company key:
ssh-add ~/.ssh/id_rsa_companyNote: On macOS, keys may be unloaded after a restart. To persist them, add
ssh-add -K ~/.ssh/id_rsa_personal(and-company) to your~/.zshrcor~/.bash_profile.
Create an SSH Config File#
An SSH config file tells your system which key to use for which GitHub connection.
-
Create/Edit the config file:
# macOS/Linux nano ~/.ssh/config # Windows (Git Bash) nano ~/.ssh/config # Windows PowerShell notepad ~/.ssh/config -
Add the following content (replace
your_personal_emailandyour_company_emailwith 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 yesHost 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).
-
Save and close the file (in
nano, pressCtrl+O,Enter, thenCtrl+X).
Add Public Keys to GitHub#
Next, add the public keys (.pub files) to your GitHub accounts.
For Your Personal Account:#
-
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 -
Click New SSH key, add a title (e.g., “Personal Laptop”), and paste the key.
-
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#
-
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 -
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]" -
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 foruser.nameanduser.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-personalalias 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-companyalias 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#
- Open VS Code.
- Open your personal repo:
File > Open Folder > ~/projects/my-blog. - Make a test change (e.g., edit
README.md). - 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:
- Open the company repo in VS Code:
File > Open Folder > ~/projects/company-app. - 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>" -1in 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 -lto list keys). If not, re-runssh-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-rungit config user.email "correct_email". - Ensure you didn’t set a global
user.email(rungit config --global user.emailto check).
SSH Agent Not Running#
- On Windows: Start the agent with
Start-Service ssh-agent(PowerShell) oreval $(ssh-agent -s)(Git Bash). - On macOS/Linux: Add
eval "$(ssh-agent -s)"to your~/.bashrcor~/.zshrcto 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#
-
Create a personal config file:
nano ~/.gitconfig-personalAdd:
[user] name = John Doe (Personal) email = [email protected] -
Create a company config file:
nano ~/.gitconfig-companyAdd:
[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!