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
2. Check if you already have an SSH key
If you already have an SSH key, you can use it or generate a new one.
3. Generate a new SSH key
- -t: Indicates the type of key you want to generate. In this case, ed25519.
- -C: It is a comment that allows you to identify the key. In this case, the email account associated with the GitHub account.
- -f: Indicates the name of the file where the key will be saved. In this case, you should use the GitHub username.
If these commands ask you for a passphrase, you can leave it blank and press enter.
4. Start the SSH agent
After this command you should see a message similar to Agent pid 1234
5. Add the SSH keys to the agent
6. Add the SSH keys to your GitHub accounts
First, open the .pub file with VSCode or Vim and copy the keys.
Then, go to Github → Settings → Keys or follow this link and click on "New SSH key".
Do this for each GitHub account, adding the corresponding SSH key.
7. Create a config file
Add the following configuration to the file, replacing personal_github_username with your personal Github username.
- Go to the home directory and create these three files
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:
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.
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.
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