Managing multiple GitHub accounts from a single computer can be a challenge, especially when it comes to ensuring that each account uses its own SSH key for authentication. This guide will walk you through the steps to set up multiple GitHub accounts on a Windows system, each with its own SSH key, and how to configure Git to use the correct keys for each account.
Step 1: Generate SSH Keys
First, you’ll need to generate a unique SSH key for each GitHub account. Here’s how to do it:
- Open Git Bash: You can download it from the official Git website if it’s not already installed.
- Generate SSH Key: Run the following command, replacing
your_email@example.com
with the email associated with your GitHub account:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Save the Key: When prompted, save the key to a path like
C:\Users\YourUserName\.ssh\github_accountname
. Replacegithub_accountname
with something that helps you identify the key’s purpose. - Repeat for each GitHub account.
Step 2: Add SSH Keys to GitHub
Once you’ve generated your keys, you need to add them to your GitHub accounts:
- Copy Key: Open the public key file (
.pub
file) you created and copy its contents. - Add to GitHub:
- Go to GitHub.com, log into your account.
- Navigate to Settings > SSH and GPG keys.
- Click on New SSH key, paste your key into the field, and save.
Step 3: Configure the .ssh/config File
This file allows you to create aliases for your SSH configurations:
- Create/Edit Config File: Open or create the
config
file in your.ssh
directory. - Add Configuration for Each Account:
# Account A
Host github.com-accountA
HostName github.com
User git
IdentityFile C:/Users/YourUserName/.ssh/github_accountA
IdentitiesOnly yes
# Account B
Host github.com-accountB
HostName github.com
User git
IdentityFile C:/Users/YourUserName/.ssh/github_accountB
IdentitiesOnly yes
Replace placeholders with actual account names and path.
Step 4: Test Your SSH Configuration
Test the SSH connection to ensure that your configuration is correct:
ssh -T git@github.com-accountA
ssh -T git@github.com-accountB
You should see a message from GitHub confirming that you’ve successfully authenticated but do not have shell access.
Step 5: Configure Git Remote URLs
To use the correct SSH keys, update the Git remote URLs in your local repositories:
git remote set-url origin git@github.com-accountA:username/repo.git
Replace username/repo.git
with the repository’s path and accountA
with the appropriate account alias.
Final Thoughts
With this setup, you can seamlessly switch between GitHub accounts by using the corresponding SSH aliases in your commands. This approach avoids confusion and authentication issues, especially when dealing with private repositories across different accounts.
By following these steps, you can maintain a clean and organized environment for managing multiple GitHub accounts from a single Windows machine.