Post

Managing Multiple GitHub Accounts Using SSH Keys in WSL and VS Code

A guide on managing multiple GitHub accounts using SSH keys in a WSL and VS Code environment.

Maybe this method is not exactly what you are searching for, but hopefully, this post will help you understand how to better manage your commits from different GitHub accounts using SSH keys.

Before actually replicating these steps, read the whole article and be sure you understand that this method is not yet perfect, as in you’ll still need to log out/in each account when using VS Code, and that you’ll also have to run git push from WSL (preferably) after committing from VS Code UI.

We’ll use WSL and VS Code in tandem in order to properly commit and sync to the right repo with the correct id/SSH key pair.

Another piece of advice (from GitHub’s docs itself) while using multiple SSH authentication pairs: do not create password-protected keys. However, I’m using both.

I based my solution on these links:

  • https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer
  • https://joshcgrossman.com/2022/01/26/getting-multiple-github-accounts-on-one-linux-wsl-machine/
  • https://joshcgrossman.com/2023/01/03/getting-multiple-github-accounts-on-one-linux-wsl-machine-2023-update/
  • https://blog.gitguardian.com/8-easy-steps-to-set-up-multiple-git-accounts/#stepbystep
  • https://medium.com/the-andela-way/a-practical-guide-to-managing-multiple-github-accounts-8e7970c8fd46
  • https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/

Summary

Separate the logic of accessing each account into directories, e.g.:

1
2
3
/home/USER/_ghSelf
/home/USER/_ghBiz01
/home/USER/_ghBiz03

Create ~/.gitconfig, ~/_ghSelf/.gitconfig.self, ~/_ghBiz01/.gitconfig.biz01, and ~/_ghBiz02/.gitconfig.biz02 files.

1
2
3
4
5
6
7
8
9
10
11
12
13
# ~/.gitconfig

[includeIf "gitdir:~/_ghSelf/"]
path = ~/_ghSelf/.gitconfig.self

[includeIf "gitdir:~/_ghBiz01/"]
path = ~/_ghBiz01/.gitconfig.biz01

[includeIf "gitdir:~/_ghBiz02/"]
path = ~/_ghBiz02/.gitconfig.biz02

[core]
excludesfile = ~/.gitignore

For example, ~/_ghSelf/.gitconfig.self:

1
2
3
4
5
6
7
8
9
[user]
email = email@domain.com # your GitHub email acc
name = Self Name # your name

[github]
user = "selfName" # your GitHub user login

[core]
sshCommand = "ssh -i ~/.ssh/id_ed25519selfName"

Create the keys

1
ssh-keygen -t ed25519 -C "email@domain.com" -f ~/.ssh/id_ed25519<selfName>

ED25519 is recommended for security and performance, but RSA with a sufficient length is also a valid alternative:

1
ssh-keygen -t rsa -b 4096 -C "01@biz.com" -f ~/.ssh/id_rsa_<Biz01>

Edit the ~/.ssh/config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Host github.com-self
  HostName github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519selfName
  User git

Host github.com-biz01
  HostName github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519biz01
  User git

Host github.com-biz02
  HostName github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519biz02
  User git

Login to the GitHub account, navigate to Settings > SSH and GPG Keys > New SSH Key and paste the .pub contents from the corresponding ID pair (repeat for every account).

Switch between accounts

To switch between accounts, navigate into the desired directory corresponding to the GitHub ID, load, and verify the correct key:

1
2
3
eval "$(ssh-agent -s)"
ssh-add -l
ssh-add ~/.ssh/id_ed25519biz01

Authenticate the keys with GitHub using the following commands:

1
2
3
4
$ ssh -T github.com-self
  # Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T github.com-biz01
  # Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

Test on GitHub repository

Head back over to your personal GitHub account. Create a new repository with your desired project name, say, test-ssh. Follow the steps below to clone the repository, create your first commit, and push to GitHub:

1
2
3
4
5
6
7
$ git clone git@github.com-personal:USERNAME/test-ssh.git
$ cd test-ssh
$ touch index.html
$ echo "Hello World" >> index.html
$ git add .
$ git commit -m 'Add index file'
$ git push origin master

It’s recommended to use the SSH option when cloning repos from now on.

You can also log in to GitHub with VS Code, verify the user and key pair in use to use the Commit button in the Source Control area, and run git push to sync the repo.

Don’t forget to change the default browser to match the logged account (if you, like me, keep users logged in with different browsers).

I hope this info helps you better isolate and manage your repos on GitHub. Have a good one!

This post is licensed under CC BY 4.0 by the author.