Yingqi
Yingqi

Reputation: 1515

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

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?

Upvotes: 110

Views: 154723

Answers (12)

VonC
VonC

Reputation: 1323553

microsoft/vscode issue 229421, following issue 127967 is clear:

Multiple GitHub account will be enabled by default.

In order to log in to a second account you either need to:

  • Start a log in flow to a new extension or feature (Settings Sync, Cloud Changes, Tunnels)

  • Go to this quick pick and choose the new Use a new account... option (not pictured, but it'll be there)

  • Change your account in Settings Sync: see "Switching Accounts".

For instance, for the GitHub Copilot Chat VSCode extension:

https://private-user-images.githubusercontent.com/2644648/369511676-d1cb38cc-a61b-470c-b9d3-c98fac20f19d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjkzNTMwMDAsIm5iZiI6MTcyOTM1MjcwMCwicGF0aCI6Ii8yNjQ0NjQ4LzM2OTUxMTY3Ni1kMWNiMzhjYy1hNjFiLTQ3MGMtYjlkMy1jOThmYWMyMGYxOWQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MTAxOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDEwMTlUMTU0NTAwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZGU0YmMwM2ZhNWQyNzQ0YzBhN2NmNTRmYzkxODRjNDk5Y2VjODIxNmM3MTk5ZTVhNzA1Y2E1Yzc0ODVjYjgyYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.eRrPjX9UEwmWy-9D55cSRAS7QnpPzI1f388lOipXcYs

This is available with VSCode insiders, and will be released with VSCode 1.95, October 2024 edition.

Upvotes: 0

djd
djd

Reputation: 173

Frustrating that even in 2024, using multiple Github accounts is difficult on VSCode. You start the sign-in on VSCode, but actually sign in using a Github webpage. Then this is login is set up and signing out from the VSCode accounts button doesn't remove the Github sign-in. Signing into Github with VSCode sets a credential in Windows. This credential can be deleted from the Windows control panel credentials manager, but that's not ideal. Also note that changing the git config information (git config --global user.name "username" git config --global user.email "email@email") doesn't actually change the git account.

The workaround I've found is using git config --global credential.github.com.useHttpPath true which seems to force VSCode to require sign in for each repository. This works as long as you're switching between repository folders, not if you're working on the same repository folder with different accounts. Then use a VSCode extension that manages git info to quickly switch the user.email and user.name

Upvotes: 1

Thainara França
Thainara França

Reputation: 21

My boyfriend and I sometimes use each other's laptops, so we decided to install Vscode Insiders. On my notebook he uses vscode insiders and on his notebook I use it. I found it simpler, and this way we could use our settings from anywhere.

Upvotes: 2

AKUMA no ONI
AKUMA no ONI

Reputation: 11819



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 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": "[email protected]",
              "user.name": "jD3V"
          },
          {
              "user.email": "[email protected]",
              "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 that's 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 "[email protected]"

~$ git config --local user.name "urGitHubUsrName"
~$ git config --local user.email "[email protected]"


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 prefer 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 creates 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 company, 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!


Upvotes: 64

Lucas Samberg
Lucas Samberg

Reputation: 31

Log to one of your accounts in github.com and on the profile photo, click and Add Account. Log in to your other account and that's it. Now VSCode will display both accounts on the menu and you can have for example, git in one account and copilot enabled in another.

Upvotes: 1

Dhana Sekaran
Dhana Sekaran

Reputation: 3

Use another laptop or pc. It is working for me.

Upvotes: -10

laggingreflex
laggingreflex

Reputation: 34627

You can run two different instances of VS Code.

On windows you can create user "x" and run:

runas /user:x code.exe

You might run into an issue where it can't open a browser window to log you in, in which case there's an option to cancel and login manually by pasting a code on github.

Upvotes: -1

John Doe
John Doe

Reputation: 101

As of June 27, 2023, it is possible to have two users active at once in both Visual Studio and Visual Studio Code, using the latest release of Git-Credential-Manager (aka GCM) version (2.2.0). Git has been integrated into VS and VSCode since their 2019 releases, with updates using GCM to handle sites like GitHub that require increased credentials.

Per the documentation :

Multiple users

If you work with multiple different identities on a single Git hosting service, you may be wondering if Git Credential Manager (GCM) supports this workflow. The answer is yes, with a bit of complexity due to how it interoperates with Git.

Version 2.2.0 is in Pre-Release as of this writing, so keep that in mind. The release is available here. You will need to install this yourself, but once you do so it should work with VS/VS Code automatically. Whenever you do a pull/push/fetch (etc), and your repository's origin does not include the username, you will see a prompt asking you what account you wish to use for authentication. You can select from any credentials previously stored or add a new one for a different account.

Other tools, notably Git for Windows, may require a bit more finagling, but that's out of scope here.

For more documentation on how this is implemented, including the new command-line options available, please check out the pull request that added this feature

Upvotes: 9

techSage
techSage

Reputation: 351

Here is a fresh approach I put together and tested successfully for those that are using Git Credential Manager (or that can use it as part of the solution; it allows Git to store creds in various OS-level stores on Windows, Linux and Mac), since I didn't love the approach in GitHub's documentation and this yields an unlimited number of accounts that can be used:

Set the credential.namespace for each repo that requires one account to one key, other repos that require another account to another key, etc (or just one less than the number of accounts you need to use and the other can be stored as default without a namespace). This keeps the credentials for each account separate in the OS Credential Manager via namespaces that you create by just specifying a key/name. You will be prompted for credentials the first time after you configure this, but it will direct each repo to use the correct credentials and keep them from getting overwritten when you authenticate to GitHub for perhaps a new account without specifying a namespace (default).

Setting the credential.namespace can be done in many ways just as other Git config settings. A simple way for a single user is to just run:

git config credential.namespace "uniquenamespace"

in each repo you need to use a different account for than the default one. You can also use a .gitconfig file in the repo root and then specify that you want it to be used for that repo on your system by running:

git config --local include.path .gitconfig

You can even semi-automate this configuration per-repo/machine for other users using Git Hooks as demonstrated here. Note: On Windows, you likely need to add the following as the first line of your post-checkout or other hook action file(s): #!/bin/sh

Upvotes: 2

Reincoder
Reincoder

Reputation: 119

Tried a bunch of options, but settled on this strategy. I am using a Windows machine.

  1. Install WSL
  2. Install VSCode Insiders Build (I think you can get away with using only one VSCode instance, but I wanted to use two)
  3. Setup one of your git config on your WSL terminal
  4. Follow the WSL on VScode instructions and install the remote developement extension pack on your insider version of VS Code.
  5. Open VSCode (insiders) from your WSL terminal using the command code-insiders or you can open VSCode (Insiders) then press ctrl+shift+p for opening the command palette then running the command WSL: Open Folder in WSL

That is my solution so far. I wish there was a toggle to switch profiles entirely as of writing this comment, I wasn't able to find a solution.

There is also the Git Config Profile Extension but I wasn't able to make it work. I think I have to set up the git config files properly.

Upvotes: 0

Fabien DESPREZ
Fabien DESPREZ

Reputation: 129

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 [email protected]
➜ 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 [email protected]

Et voilà !

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

Upvotes: 12

supi
supi

Reputation: 2272

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.

Upvotes: 2

Related Questions