Managing multiple GitHub accounts in Visual Studio Code (VS Code) efficiently involves setting up SSH keys and configuring your Git environment to use the correct account for each project. This approach allows you to seamlessly switch between your personal and work repositories without constant re-authentication.
How to Use Multiple GitHub Accounts in Visual Studio Code?
To use multiple GitHub accounts in Visual Studio Code, you'll primarily leverage SSH keys and Git's configuration to distinguish between your accounts for different projects. This ensures that commits are attributed to the correct GitHub profile and that you have proper access to your repositories.
1. Generating Separate SSH Keys for Each GitHub Account
The foundation for managing multiple accounts securely is to create a unique SSH key pair for each GitHub account. This provides a distinct identity for each account when interacting with GitHub.
- Open your terminal or Git Bash.
- Generate the first SSH key pair (e.g., for personal account):
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_personal
When prompted, you can choose to enter a strong passphrase for added security or leave it empty if you prefer.
- Generate the second SSH key pair (e.g., for work account):
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_work
Again, you can add a passphrase.
- Repeat for any additional accounts, using a distinct filename for each (e.g.,
id_rsa_account3
).
Adding SSH Keys to the SSH Agent:
For your system to use these keys automatically, you should add them to your SSH agent:
- Start the SSH agent:
eval "$(ssh-agent -s)"
- Add your private keys:
ssh-add ~/.ssh/id_rsa_personal ssh-add ~/.ssh/id_rsa_work
You might be prompted for your passphrase if you set one.
2. Adding SSH Public Keys to Your GitHub Accounts
For each GitHub account, you need to add the corresponding public SSH key to its settings.
- Copy the public key for your personal account:
cat ~/.ssh/id_rsa_personal.pub
Copy the entire output.
- Go to your personal GitHub account settings (usually
github.com/settings/keys
). - Click "New SSH key", give it a descriptive title (e.g., "VS Code Personal"), and paste the copied public key.
- Repeat for your work account:
cat ~/.ssh/id_rsa_work.pub
Copy this output.
- Go to your work GitHub account settings and add this key with an appropriate title (e.g., "VS Code Work").
3. Configuring SSH for Multiple Accounts
Create or edit the ~/.ssh/config
file to instruct SSH which key to use for which GitHub identity. This file acts as a directory for your SSH connections.
-
Create or open the
~/.ssh/config
file:touch ~/.ssh/config # Or open it with a text editor: code ~/.ssh/config
-
Add the following configurations to the file:
# Personal GitHub Account Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_personal IdentitiesOnly yes # Work GitHub Account Host github.com-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_work IdentitiesOnly yes
Explanation:
Host github.com-personal
: This creates an alias for your personal account. You'll use this alias in your Git remote URLs.HostName github.com
: Specifies the actual GitHub domain.User git
: Alwaysgit
for SSH connections to GitHub.IdentityFile ~/.ssh/id_rsa_personal
: Points to the specific private key to use for this alias.IdentitiesOnly yes
: Ensures that only the specified identity file is used, preventing unintended key exposure.
4. Testing SSH Connections
Before proceeding, verify that your SSH connections are working correctly for each alias.
- Test personal account:
ssh -T [email protected]
You should see a message like "Hi your_personal_username! You've successfully authenticated, but GitHub does not provide shell access."
- Test work account:
ssh -T [email protected]
You should see "Hi your_work_username! You've successfully authenticated, but GitHub does not provide shell access."
If you encounter errors, double-check your SSH key paths, public key uploads to GitHub, and the ~/.ssh/config
file syntax.
5. Configuring Git Remotes in VS Code (or Terminal)
The final step is to tell Git (and thus VS Code) which SSH alias to use for each repository.
-
For a new repository:
When cloning a repository, use your customHost
alias in the clone URL:- Personal repo:
git clone [email protected]:your_personal_username/your-repo.git
- Work repo:
git clone [email protected]:your_work_username/your-repo.git
- Personal repo:
-
For an existing repository:
If you already have a repository cloned using the default[email protected]
, you need to update its remote URL.- Open the repository in VS Code.
- Open the integrated terminal (Terminal > New Terminal).
- Check the current remote URL:
git remote -v
- Update the remote URL to use your SSH alias:
- For a personal project:
git remote set-url origin [email protected]:your_personal_username/your-repo.git
- For a work project:
git remote set-url origin [email protected]:your_work_username/your-repo.git
Now, when you push, pull, or fetch from this repository within VS Code, Git will automatically use the specified SSH key associated with that alias.
- For a personal project:
6. Setting Local Git User Information
For each repository, it's crucial to set the correct Git user name and email to ensure your commits are attributed to the right GitHub account.
- Navigate to your repository's root directory in the terminal.
- Set the user name and email for that specific repository:
- For personal project:
git config user.name "Your Personal Name" git config user.email "[email protected]"
- For work project:
git config user.name "Your Work Name" git config user.email "[email protected]"
These settings are stored in the
.git/config
file of the repository and override any global Git configurations.
- For personal project:
Alternative: Using HTTPS with Git Credential Manager (GCM)
While SSH is often preferred for multiple accounts due to its key-based system, you can also use HTTPS. This requires a different approach, often utilizing a Git Credential Manager (GCM) which helps manage multiple personal access tokens (PATs) or OAuth tokens for different accounts.
When using HTTPS:
- Generate a Personal Access Token (PAT) for each GitHub account from their respective GitHub settings (Developer settings > Personal access tokens). Grant the necessary scopes (e.g.,
repo
). - When cloning a repository via HTTPS, Git will prompt for your username and password/PAT.
- Use Git Credential Manager (GCM): GCM (built into Git for Windows, available for macOS/Linux) can store different credentials for different remote URLs. It will cache your PATs. For managing multiple accounts, you'd typically have to manually manage which PAT is active or use environment variables/wrapper scripts to select the correct PAT before interacting with a specific repository if the URLs are identical (e.g., both point to
github.com
). However, for distinct organizations or users, GCM can often handle unique prompts.
Recommendation: For managing multiple personal and work GitHub accounts, the SSH method described above is generally more straightforward and robust within VS Code as it relies on distinct SSH aliases configured directly in your system.
Summary Table: Key Differences in Configuration
Aspect | SSH Configuration | HTTPS with GCM (Alternative) |
---|---|---|
Authentication Method | SSH keys (.ssh/id_rsa_personal , id_rsa_work ) |
Personal Access Tokens (PATs) |
Setup | Generate keys, add to GitHub, configure ~/.ssh/config |
Generate PATs, ensure GCM is installed, manual entry/caching |
Remote URL Format | [email protected]:user/repo.git |
https://github.com/user/repo.git |
Switching Accounts | Handled by unique Host aliases in ~/.ssh/config |
Managed by GCM storing credentials, or explicit PAT use |
Security | Strong encryption, passphrase option, key management | Token-based, scopes can be restricted |
Complexity | Initial SSH config, then seamless for each repo | Requires understanding PATs, GCM behavior |
By following these steps, you can effectively manage and use multiple GitHub accounts from within Visual Studio Code, ensuring proper attribution and access for all your projects.