Reputation: 1515
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.
Upvotes: 110
Views: 154723
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:
This is available with VSCode insiders, and will be released with VSCode 1.95, October 2024 edition.
Upvotes: 0
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
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
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.
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.
ANSWER #1
(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.
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.
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"
}
]
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.
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.
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.
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]"
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 creates 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 company, meaning you will have to use certain settings. This method will prevent you from having to change your personal configuration.
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
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
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
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
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
Reputation: 119
Tried a bunch of options, but settled on this strategy. I am using a Windows machine.
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
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)
~/.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
~/.ssh/id_rsa_perso.pub
~/.ssh/id_rsa_pro.pub
(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
➜ 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
(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]
You can now manage your repositories both from the command line and VSCode GUI !
Upvotes: 12
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:
and let's say you work from Ubuntu and windows interchangeably. Then you would need to create 4 keys as follows:
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.
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.
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