21

I have 2 different GitHub accounts: A personal account for my own needs, and a company GitHub account that I manage. I use V.S. Code for both company & personal projects, and I was hoping to find an answer to the following question.

Is it possible to log into two GitHub accounts at the same time in V.S. Code? If not, then does someone know if V.S. Code has a quick and easy mechanism built into its UI for switching between GitHub accounts?

j D3V
  • 4,063
  • 3
  • 21
  • 51
Yingqi
  • 547
  • 1
  • 7
  • 19
  • 1
    Would [this](https://stackoverflow.com/a/57771362/11247175) work for you? – hotpink Jun 28 '20 at 17:04
  • 1
    VSCode uses the git settings of your git install, try some of these https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer – tHeSiD Jun 28 '20 at 17:05
  • Is there anyway I can get you to select a solution for this question? – j D3V Jul 17 '21 at 00:24
  • @Yingqi Can you select an answer as being correct for this question? – j D3V May 21 '22 at 23:09

3 Answers3

10
Edit 1: "2021-06-06"     •     Edit 2: "2021-07-15"     •     Edit 3: "2022/05/21"

EDITORS NOTE: This question has been edited several times to keep it useful, and current. VS Code version releases (which happen on a monthly basis) have changed several things I included in the answer, like the location of certain things, and how to do certain things. Of course, no of that matters now, but it would have, had you tried to use an answer that didn't work. What's most important now, is that as of "2022/05/21", this is a working answer.




Lets Start by Answering Your 2 Questions



QUESTION 1

  • 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.

Question #2

  • (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 INSTALLING - This 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!


j D3V
  • 4,063
  • 3
  • 21
  • 51
  • Has the Account Context Menu been removed? I'm running the latest version of VSCode at the time of writing this and I don't have the option to add another account or to log in to another account. – Gal Grünfeld Oct 02 '21 at 14:48
  • @GalGrünfeld No, I had trouble with GitHub not logging into its Issues & Pulls extension yesterday, and I had to click on it and use it. – j D3V Oct 02 '21 at 15:09
  • Did you click the accounts option like I showed in the photo? You have to have it selected or it wont display – j D3V Oct 02 '21 at 15:10
  • 1
    I realized that you were showing how to add a menu that I already have. My issue is that it doesn't have the option to add another account other than the one I'm currently signed into, or to switch to another account from it. The only two elements it has are: One element that says account's email and type (` (Microsoft)` that has suboptions: `Manage Trusted Extensions` and `Sign Out` And another element that says `Setting Sync Is On` that onClick opens the command pallete for SettingsSync. – Gal Grünfeld Oct 03 '21 at 15:35
  • 1
    @GalGrünfeld You have to sign out Gal, and then backinto another account – j D3V Oct 05 '21 at 02:39
  • @GalGrünfeld try signing out, then sign back in. Now things may have changed since they implemented the Microsoft account as being able to store your backed up settings and Keybindings, therefore; if signing out doesn't work for you let me know, and I will see if I can't recreate the issue, and update the question. (__SIDENOTE:__ Someone else used this method a few weeks ago, and they were successful, this is the intended microsoft method for switching accounts). – j D3V Oct 05 '21 at 02:44
  • @GalGrünfeld Like I said, try signing out, and signing in to a new account, and if you are succefful, feel free to update the answer above, with anything I missed. Like I said, if you are not, I will unbind my __GitHub__ account and go through the process to figure out if somthing in the account login process has changed. – j D3V Oct 05 '21 at 02:46
  • How does the answer explains how to setup two accounts? The user and email git setting, IMHO, has nothing to do with GitHub. The "built-in" "GitHub" (not Pull and Issues) extension keeps credentials for one account and it doesn't appear on the Account button. – AlexVB Jan 22 '22 at 21:50
0

Was struggling with this too, but found an easy, natural solution. Tested with VSCode 1.61.1 and 1.61.2 (Windows 11) and WSL2 (Alpine)

Define some aliases in ~/.ssh/config

Host *github*
    HostName github.com
    User git
Host perso-github.com
    IdentityFile ~/.ssh/id_rsa_perso
Host pro-github.com
    IdentityFile ~/.ssh/id_rsa_pro

Make sure you ssh keys are added in your github accounts:

  • Personal Account (git_perso_account) : ~/.ssh/id_rsa_perso.pub
  • Professional Account (git_pro_account) : ~/.ssh/id_rsa_pro.pub

Clone your repositories as follows

(or edit their .git/config afterwards to make sure their remote "origin" url matches the alias defined in your ~/.ssh/config file)

➜ ~ # cd repositories
➜ repositories # git clone perso-github.com:git_perso_account/some_fun_repo.git 
➜ repositories # git clone pro-github.com:git_pro_account/boring_business_repo.git 

Remove git global user config

➜ repositories # git config --global --unset user.name
➜ repositories # git config --global --unset user.email

Note: It might not be necessary as git --local settings are supposed to override --global settings

Add your github account informations in each repository

(Git-Autoconfig might help here in the future)

➜ repositories # cd some_fun_repo 
➜ some_fun_repo git:(master) # git config --local user.name git_perso_account
➜ some_fun_repo git:(master) # git config --local user.email perso@gmail.com
➜ some_fun_repo git:(master) # cd ../boring_business_repo
➜ boring_business_repo git:(master) # git config --local user.name git_pro_account
➜ boring_business_repo git:(master) # git config --local user.email you@business.com

Et voilà !

You can now manage your repositories both from the command line and VSCode GUI !

  • Well. I also see that the link https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer provided by tHeSiD in comment to the initial question gives the correct answer. Too bad it's not more visible... – Fabien DESPREZ Oct 20 '21 at 08:37
-1

Idea: Configure git in the folder where you work on your PC with SSH keys to authenticate your pulls or pushes. You will need to therefore create SSH keys for each of your Github account for each of your PC.


Let's say you have 2 Github accounts:

  1. work_github
  2. personal_github

and let's say you work from Ubuntu and windows interchangeably. Then you would need to create 4 keys as follows:

  1. key_generated_on_windows_for_accessing_work_github_from_windows
  2. key_generated_on_windows_for_accessing_personal_github_from_windows
  3. key_generated_on_ubuntu_for_accessing_work_github_from_Ubunutu
  4. key_generated_on_ubuntu_for_accessing_personal_github_from_Ubunutu

Step 1: create SSH keys

for windows(use powershell or gitbash or vscode terminal) and ubuntu

ssh-keygen -t rsa -C "your_email"

Step 1.1: when you are faced with this question:

Enter file in which to save the key (/c/Users/your_username/.ssh/id_rsa):

enter an identifiable name, with the full path. For e.g.:

/c/Users/your_username/.ssh/id_rsa_yourname_work_github

you can skip the passphrase by leaving those empty

Repeat this step for both the github accounts for each PC


Step 2: add your public key to github

creating ssh key creates 2 files.

  1. One public - for e.g. /c/Users/your_username/.ssh/id_rsa_yourname_work_github.pub
  2. and One private - for e.g. /c/Users/your_username/.ssh/id_rsa_yourname_work_github

open the .pub file in your favorite editor and copy the entire contents of file including your email.

Go to your github account > Settings > Deploy keys > Add deploy key

paste the copied contents into the "key" text field and give it a recognizable title. for e.g. "windows_your_username" so you know this key will authenticate you from windows PC

Add deploy keys in both the github accounts for all the keys you created on both the PCs.


Final step: configure local git to use the right key and test the access

  1. go to your working folder (or the cloned folder)

  2. open terminal (or powershell) and make VScode(refer here for other editors) the default editor to open git config file

    git config --global core.editor "code --wait"

  3. open git config file

    git config -e

  4. under [core], add the ssh command with private key path, for e.g.

    sshCommand = ssh -i ~/.ssh/id_ra_yourname_work_github

  5. add [user] and add your github username and email

  6. point the remote url to your repository if it isn't already so

enter image description here

You can also use the git command line tool to configure these, instead of editing it in an editor.

  1. that's it. Make a change, commit and try pushing it to github repo.

Repeat this step for all working folders on your PC.


You should now be able to work with different Github accounts on the same system

Confession: All credits for this answer is attributed to this article here.

supi
  • 1,982
  • 17
  • 15