I have 2 GitHub accounts. How can I use both when I am working in VS Code?


LIST of MAJOR REVISION DATES

     
EDIT 1: 2021-06-06
      •      
EDIT 2: 2021-07-15
      •      
EDIT 3: 2022/05/21

This question has been edited several times to keep it useful, and current. The most recent edit was on September 14th of 2022 at approx 10am UTC. There have been 3 major edits (or revisions is probably the more appropriate term to use here), that were necessary in order to keep this answer relevant and up to date. The revisions were prompted by changes to the UI and changes to GitHub’s own defined protocol that defines the login process (I am specifically referring to GitHub now requiring a PAT security token to be used to authenticate anything pushed using the HTTPS protocol). The most important thing to note is as of 2022/09/14, this is a working answer.


This Q&A Contains 2 Questions.

The first question can be answered very quickly, whereas, Question-#2 will require far more explanation and contains the answer that you’ll likely be looking for.

The 1st question (“Question #1”) asks:

  • Is it possible to log into two GitHub accounts at the same time in V.S. Code?

ANSWER #1

  • No! You Cannot Log into 2 GitHub Accounts at the Same Time.


The 2nd question (“Question #2”) asks:

  • (Q) Does V.S. Code have a quick & easy mechanism built into its UI for switching between GitHub accounts?

  • (A) YES IT DOES! VS Code has an icon on the “activity-bar” that can be used for logging in & out of your GitHub & Microsoft accounts. I will go into detail about this below.

Step-1: Install the Following Extensions


You should have a couple extensions that help to automate the login process, which now requires a PAT (even in the CmdL). And that also help with organizing the user.name and user.email that you use for each project. Bellow are the 2 extension you need.


  1. “GitHub Pull Requests & Issues Extension”
  • Link to open in VS Code: vscode:extension/GitHub.vscode-pull-request-github

  • REASON 4 INSTALLINGThis is a GitHub extension that everyone using GitHub should use, but it also does something to help with Terminal integration that enables UI and CLIs to co-exist. The last sentence was nearly word for word from the extensions description. To be more concise, installing it will help to automate the login process to GitHub. Upon installing it will likely prompt you to log into GitHub. If that is the case, then YES LOG IN

  1. GIT-AutoConfig
  • _”Git-Autoconfig automatically sets credentials for each new project you clone, &/or create. It also prompts you when you open your projects to make sure that the project is configured with the correct user.name & user.email. When using two GitHub accounts it is critical that you configure each project specifically for the account it belongs to. If a project accidentally gets configured for the wrong project, bad things happen, and lots of time is wasted.

  • This extension needs to be configured. The extension attempts to auto-configure each project, but in-case, you feel a project isn’t configured, or is configured incorrectly, you can hit the F1 key to drop open the “Quick Input Menu”. With the menu open, type “git autoconfig set”. When you see an option that reads, “Set Config”, select it, and follow the 2-part instructions.

A HELPFUL TIP:

When “Git-AutoConfig” prompts you, if the setting below is configured in your user-scoped "settings.json" file, then you can select a pr-configured input, rather than manually typing the input each time.

    /**
     * @file "settings.json"
     * @desc "This is what my configuration looks like" */

    "git-autoconfig.configList": [
         {
              "user.email": "W3Dojo@gmail.com",
              "user.name": "jD3V"
          },
          {
              "user.email": "AndrewJay.zck@gmail.com",
              "user.name": "JAYD3V"
          }
    ]


Restart V.S. Code

Before continuing to Step-2, please restart your editor, so the menu we use below recognizes the GitHub extension you installed.

Step-2: Configuring your Git settings using the Git CLI


We are going to continue forward from the Welcome Page, as being in a project configured for the wrong GitHub account can screw something up. FYI, you want to make sure you are logged into the correct account when bouncing from one project to the next. You don’t want to enter a project for one account, when your logged into another account. It can really confuse VS Code, and even more so, VS Code Extensions. Oh, and Git, the software that VS Code uses to make all of this work.

To get to the Welcome page, preform the following steps:
  1. Choosing “FILE” from the title-bar menu.
  2. Choose “CLOSE FOLDER” from the file-menu.
  3. You should now be at the VS Code welcome page, this is a good neutral place to start.

It might sound like I am being finicky, but I want this to work for you, and anything involving Git, can get screwed up very badly, very easily.

Step-3: The Account Context Menu


We will use the “Account Context Menu” to log out of any GitHub accounts your logged into. Click on the icon located at the very bottom of your Activity-bar.

Account Context Menu Location


The image below shows the selections to choose for logging out. If you are not logged into any GitHub accounts, then thats fine. Just carry on.


Logging out of GitHub Account using Account Context Menu


Step-4: Configuring Git

In your head, think of the GitHub account your going to log into.

Now, execute the following commands with the correct values for the account you are going to use to log in.

~$ git config --global user.name "urGitHubUsrName"
~$ git config --global user.email "urGitHubEmail@foobar.com"

~$ git config --local user.name "urGitHubUsrName"
~$ git config --local user.email "urGitHubEmail@foobar.com"

Step-5: Logging In

PLEASE NOTE: You are going to log in twice!

  1. First, Click the “Account Context Menu”
  2. With the menu open choose the option that says “Sign-in to Sync Settings” as shown in the image below.
  3. It will let you choose between signing in with Microsoft or GitHub. With this login, you can choose either, but I by far perfer to use my GitHub account, as I use Linux. Depending on what you have done in VS Code in the past, you may need to use to copy & paste a PAT for authentication.
  4. After signing in to MS/GH for syncing settings, Click the “Account Context Menu” again. This time choose “Sign-in: GitHub Pull-request & Issues”. Follow instructions. Image bellow shows both options

enter image description here


Now the reason I had you use the settings sync with the GitHub Pull-request & issues extension is for two reasons.

  1. If you use a different GitHub account for each one, you will be auto-logged out of the other. In other words, if you log into the GH extension using account A, while you already logged into settings sync with account B, account B gets logged out. This isn’t all, it creats confusion, trust me when I say you want to avoid the confusion when your working in your projects.

  2. Each GitHub account has its own settings synced for it. This includes settings, keybindings, snippets, ect… This way if you have one for work, and one for personal, you can manage different settings. Usually a Company GitHub account will be used by other people in the compant, meaning you will have to use certain settings. This method will prevent you from having to change your personal configuration.

LASTLY

To login to another account, just repeat the steps. It may seem slow the first time you do it, but I log in and out in literally, about 10 seconds now. Cheers from Nor Cal!


Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)