NL.

24 oct 2023

How to work with multiple Github accounts in VSCode using SSH keys

At the time I'm writing this post, VSCode only allows you to have one account logged in at a time. This can be a problem if you work with multiple GitHub accounts, for example, a personal one and another for work.

You can solve this with a configuration that you only have to do once and that will allow you to work with one account or another depending on the folder of the computer you are in.

In this post you will find the step by step to do this configuration.

1. Open git bash and go to the .ssh folder

cd ~/.ssh

2. Check if you already have an SSH key

ls -al ~/.ssh
# SSH keys end with the .pub extension

If you already have an SSH key, you can use it or generate a new one.

3. Generate a new SSH key

# run the commands one after the other
ssh-keygen -t ed25519 -C "personal_email_account" -f ~/.ssh/personal_github_username
ssh-keygen -t ed25519 -C "work_email_account" -f ~/.ssh/work_github_username

If these commands ask you for a passphrase, you can leave it blank and press enter.

4. Start the SSH agent

eval `ssh-agent -s`

After this command you should see a message similar to Agent pid 1234

5. Add the SSH keys to the agent

# run the commands one after the other
ssh-add ~/.ssh/personal_github_username # personal SSH key
ssh-add ~/.ssh/work_github_username # work SSH key

6. Add the SSH keys to your GitHub accounts

First, open the .pub file with VSCode or Vim and copy the keys.

# run the commands one after the other
code ~/.ssh/personal_github_username.pub
code ~/.ssh/work_github_username.pub

Then, go to Github → Settings → Keys or follow this link and click on "New SSH key".

Add SSH key to GitHub

Do this for each GitHub account, adding the corresponding SSH key.

7. Create a config file

# run the commands one after the other
touch config
code config # open the file with VSCode

Add the following configuration to the file, replacing personal_github_username with your personal Github username.

# inside ~/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/personal_github_username # Default key (you can also use the work key if you prefer)
  1. Go to the home directory and create these three files
cd ~
touch .gitconfig
touch .gitconfig.personal
touch .gitconfig.work

If you have more than two accounts, create a .gitconfig file for each one.

9. Contents of the .gitconfig files

Open the .gitconfig files and add the following content:

# inside ~/.gitconfig.personal
[user]
email = personal_email
name = Your Name

[github]
user = "personal_github_username" #must be in quotes

[core]
sshCommand = "ssh -i ~/.ssh/personal_github_username" #must be in quotes
# inside ~/.gitconfig.work
[user]
email = work_email
name = Your Name

[github]
user = "work_github_username" #must be in quotes

[core]
sshCommand = "ssh -i ~/.ssh/work_github_username" #must be in quotes
# inside ~/.gitconfig
[includeIf "gitdir:C:/Users/<your_pc_username>/Personal/"] # personal configuration for all projects in Personal/
path = ~/.gitconfig.personal

[includeIf "gitdir:C:/Users/<your_pc_username>/Work/"] # work configuration for all projects in Work/
path = ~/.gitconfig.work

[core]
excludesfile = ~/.gitignore # Ignore the files indicated in .gitignore

Save and close the files.

Now when you clone a project inside the Personal or Work folder, the corresponding configuration will be applied.

Test that everything works correctly

I'm leaving you an example of how to clone a project with each account.

# Clone a project in the Personal folder
cd ~/Personal
git clone git@github.com:personal_github_username/project_name.git

# Clone a project in the Work folder
cd ~/Work
git clone git@github.com:work_github_username/project_name.git

I recommend that you try cloning private repositories to make sure everything is working correctly.

You can also try to commit and push changes to see on GitHub that they were made with the correct accounts.

TIPS FOR COMMON ERRORS

Check that all the values to be replaced are correct and that you didn't miss any without replacing them.

1. Use the command ssh -vvvvA -T git@github.com to get a diagnostic if something goes wrong.

2. Use ssh-add -l to check the SSH keys added to the agent.

3. Use git config --list to check the git configuration settings.

4. If you are getting the error connect to host github.com port 22: Connection timed out add the code below to the config file.

# inside ~/.ssh/config
Host *
IgnoreUnknown AddKeysToAgent,UseKeychain
AddKeysToAgent yes
UseKeychain yes
Port 443
Hostname ssh.github.com
IdentityFile ~/.ssh/personal_github_username # Default key

5. Try commenting out the line IdentityFile ~/.ssh/personal_github_username inside the ~/.ssh/config file.

THANKS FOR READING 🥳

If you have any questions or feedback, you can contact me via Twitter or LinkedIn.

If you liked the post, you can share it with more developers who might find it useful.

Thank you! 🙌

Go back