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.
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.
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
_"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:
- Choosing "FILE" from the title-bar menu.
- Choose "CLOSE FOLDER" from the file-menu.
- 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.
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.
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!
- First, Click the "Account Context Menu"
- With the menu open choose the option that says "Sign-in to Sync Settings" as shown in the image below.
- 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.
- 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
Now the reason I had you use the settings sync with the GitHub Pull-request & issues extension is for two reasons.
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.
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!
<my-microsoft-account's-email-address> (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. –
Patronymic 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
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 !
ssh-add ~/.ssh/id_rsa_pro
and ssh-add ~/.ssh/id_rsa_perso
, but this solution worked to access both repos - only 1 account is still attached to VS Code (Copilot is only available in one of them). –
Parse 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:
- work_github
- personal_github
and let's say you work from Ubuntu and windows interchangeably. Then you would need to create 4 keys as follows:
- key_generated_on_windows_for_accessing_work_github_from_windows
- key_generated_on_windows_for_accessing_personal_github_from_windows
- key_generated_on_ubuntu_for_accessing_work_github_from_Ubunutu
- 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.
- One public - for e.g. /c/Users/your_username/.ssh/id_rsa_yourname_work_github.pub
- 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
go to your working folder (or the cloned folder)
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"
open git config file
git config -e
under [core], add the ssh command with private key path, for e.g.
sshCommand = ssh -i ~/.ssh/id_ra_yourname_work_github
add [user] and add your github username and email
point the remote url to your repository if it isn't already so
You can also use the git command line tool to configure these, instead of editing it in an editor.
- 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.
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
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.
Tried a bunch of options, but settled on this strategy. I am using a Windows machine.
- Install WSL
- Install VSCode Insiders Build (I think you can get away with using only one VSCode instance, but I wanted to use two)
- Setup one of your git config on your WSL terminal
- Follow the WSL on VScode instructions and install the remote developement extension pack on your insider version of VS Code.
- 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 commandWSL: 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.
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.
Use another laptop or pc. It is working for me.
© 2022 - 2024 — McMap. All rights reserved.