Introduction
It is common for a developer to have at least two GitHub accounts, one personal and one for work. Here is a straightforward way to make both work side by side on Windows.
First navigate to:
C:\Users\<user-name>\.ssh
Check whether you already have SSH keys there. If you do not, the folder looks like this:
.ssh
├── known_hosts
└── known_hosts.old
Now let us generate two SSH keys.
| Account type | GitHub username | |
|---|---|---|
| Personal | naveen | naveen@gmail.com |
| Work | codeandcloud | naveen@codeand.cloud |
This example uses Ed25519 keys.
Generating SSH keys
Open Git Bash and run:
ssh-keygen -t ed25519 -C "naveen@gmail.com"
You will be prompted for:
- The file to save the key
- A passphrase
In this example, the personal key is saved as id_personal and the work key as id_work.
~/.ssh$ ssh-keygen -t ed25519 -C "naveen@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/____/.ssh/id_ed25519): id_personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_personal
Your public key has been saved in id_personal.pub
Create the work key in the same way:
~/.ssh$ ssh-keygen -t ed25519 -C "naveen@codeand.cloud"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/____/.ssh/id_ed25519): id_work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_work
Your public key has been saved in id_work.pub
Adding SSH keys to the agent
Start the SSH agent:
eval "$(ssh-agent -s)"
Then add the identities:
ssh-add id_personal
ssh-add id_work
The terminal will look like this:
~/.ssh$ eval "$(ssh-agent -s)"
Agent pid 3350
~/.ssh$ ssh-add id_personal
Identity added: id_personal (naveen@gmail.com)
~/.ssh$ ssh-add id_work
Identity added: id_work (naveen@codeand.cloud)
Adding the SSH keys to GitHub
Copy the public key:
cat id_personal.pub
Then go to GitHub and add the key in SSH and GPG keys.
Configuring SSH for multiple accounts
Create a config file in .ssh and save:
# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
# Work GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
Testing the keys
ssh -T git@github-personal
ssh -T git@github-work
If everything works, GitHub will confirm that you authenticated successfully.
Using different accounts for different repositories
If you have a repository named hello-world in the codeandcloud account, clone it like this:
git clone git@github-work:codeandcloud/hello-world.git
The only difference is replacing git@github.com with the host alias you configured.
Happy coding.