Using Multiple github accounts and repos in Visual Studio Code and why the obstacle is the way

So, if you have multiple github accounts and repos you can access them all from VS Code, don't let others fool you, where they failed you will conquer:

Prerequisites

  • You already cloned your repos to your PC
  • You have git installed
  • You are drinking water daily, sleeping well and lifting some weights

So, here we go:

Go to your github github.com and login to your account, then go to Settings

image.png

Then, go to the Developer settings at the end of the list to the right

image.png

click Personal access tokens located at the end of the list to the right, and create a new token, just click the button Generate new token

image.png

Give the token a name or Note, set the expiration period and check the repo scope like this:

image.png

Now click the Generate token button at the form bottom and copy the token!

IMPORTANT: you have to perform the previous steps for each github account that you want to access from Visual Studio Code.

Ok, now to the batmobile!

Open the repo folder in your PC and find the folder named .git, it may be hidden so you just need to type the name on the explorer address bar. It will show some files and folders like these:

image.png

Open the config file with your favorite text editor, notepad will do, don't worry. You will see something like this:

image.png

Change the url parameter on the file with the following:

The original value is something like this:

url = https://github.com/<github username>/<repo git file name>.git

Change it to this:

url = https://<github username>:<your token>@github.com/<github username>/<repo git file name>.git

Now another step to avoid commits that use the global username and email:

Add the following lines at the end of the file:

[user]
    email = <the email address that you want to use>
    name = <the username that you want to use>

"That you want to use" means whatever, but I always put there the user and email associated with the github account.

Save the file.

Open the folder or the workspace associated to the folder in VS Code, you should be able to commit your changes without login in with your github account.

Rinse and repeat for every repo.

Vires acquirit eundo!