I want to connect vscode to two github accounts, because I have one personal account and one company account, and want to be able to push to different accounts(personal project commit to personal account, business commit to business account) when I work on different projects.
If I cannot login two different accounts, can I somehow log out the previous one and log in another one? Or do I have to use another computer? BTW, I am not sure if it matters, but I am using windows, WSL Ubuntu.
Thank you!
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.
TL;DR
UPDATE:
"As is often the inherent nature of technology, V.S. Code changed, consequently, the original answer was rendered deprecated, and was unhelpful, possibly even confusing to the users attempting to implement the answer as a solution, as it no longer worked in updated VSCode Versions. As of JULY 2021 I have edited this answer twice now, in an attempt to maintain a correct, current, and relative answer."
TL;DR
Steps 1 & 2 Include:
...a detailed description of how to configure your projects for two different GitHub accounts. and a great extension to use that helps Developers maintain the correct GitHub account information for their projects that span across multiple accounts
Quickly Find Out What You Need to Know
For a Quick Solution Just Read Step #3, if you have issues you can then go back to steps 1 & 2:
In a nutshell, Step #3 demonstrates how the Logging in & out Process, for different GitHub Accounts, works. For experienced VS Code users, and Git/GitHub users, this should be sufficient enough, and the rest is TL;DR, however I added quite a bit more, because there is a good deal of info around this topic that newer programmers will likely need.
"Not only is it possible to easily, and effectively use 2 different GitHub accounts w/ VSCode, it can be done using the V.S. Code GUI."
Firstly I assume that you have your GitHub account working with VSCode, which means you have already created a PAT in GitHub, you have downloaded the GitHub issues & Pull Requests extension, and authorized VSCode to be approved by GitHub. In other words, I assume that your issue is not to git GitHub working with V.S. Code, but rather to get two GitHub accounts working in a V.S. Code environment, where you have already got one account working.
The biggest problem people have using two accounts, is configuring each project for a different account. Often times they will get it working, just to be confused later down the road. I will share my configuration, and an extension I will use, that I personally have found to be a sound & full proof method.
First off, configure your local .gitconfig files on your machine by executing the following commands (use which ever account you consider to be your primary account, or if one account isn't yours, then use your account to execute the commands below).
.gitconfig file setup, which means that you probably don't need to have one.~$ 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"
~$ git config --worktree user.name "urGitHubUsrName"
~$ git config --worktree user.email "urGitHubEmail@foobar.com"
Acquire the following extension from the V.S. Code marketplace.
Git-Autoconfig forces developers to configure git at the start of every project, you will be asked for your Git Credential every time you open, create, or clone a new project. If you have a project it hasn't configured for you that was preexisting, it has a couple commands to notify it to configure the .gitconfig for that project with your email and GitHub username.
The Reason I prefer to use Git-Autoconfig is that it always configures Git correctly. I never forget to assign the correct email and username to a project, and it opens a textbox at the top of the editor for you so you never have to use the Git CLI.
EDIT: (Added the paragraph below July-16th-2021)
I found, what is IMO, the best way to configure the Git-Autoconfig extension. Add the configuration in the code block below to your settings.json file. The configuration is an array of JS objects. Each object represents a Git account, or in this case, a GitHub account. The objects have two properties, that are typical settings for standard .gitconfig files:
1. "user.email"
-AND-
2. "user.name"
Use the configuration as seen below to add your 2 (or 3, or 4,etc...) accounts to the Git-Autoconfig Extension.
//
"git-autoconfig.configList": [
{
"user.email": "DevJay@Dyslexia.com",
"user.name": "DevJay"
},
{
"user.email": "JayDev@isAwesome.com",
"user.name": "JayDev"
}
]
NOTE: "The configuration above enables Git-Autoconfig to allow you to choose a git configuration from a selection that contains the correct settings for your github accounts. Git-Autoconfig knows what your account settings are, because you configured Git-Autoconfig to know, by entering the correct information in the configuration above. If this sounds a little confusing, that is because at first it is. If you just use the git CLI, and the command git config user.email you will only ever be able to set a configuration for one account at a time. Git-Autoconfig gets around this by auto configuring a .gitconfig file, that's in every project directory when using VSCode. If you configure, and use Git-Autoconfig correctly, then you don't really need to understand the rest."
the start of every newly created project, or at the every at the start of every project, with the option to select the proper configuration for the correct GitHub account for the new project."_
I want to touch on this extension quickly before moving on. Personally I feel this extension should be called GITHUB SUPPORT rather than "GitHub Pull Requests and Issues", as "GitHub Pull Requests and Issues" doesn't really do the extension justice, in terms of making it known that it is an important app for GitHub users to have. It has the mechanisms needed to help users through the GitHub PAT authorization process, as well as other important stuff. Much of what this extension offers can be used through the command-line, however, this extension lets users use GitHub features through the V.S. Code UI, or rather GUI.
Here is your bread, and butter. This is what most people miss, and the info most people are loogin for, and it is unbelievably simple, yet people overlook it a lot.
There is a "not so well known" tool/feature that V.S. Code VSCode Tool/Feature that VSCode documents poorly, as it is in a single place, "which is a release note that is roughly 18 month old". I took me a long time to find anysort of reference, so I could know the correct semantics when referencing it.
You can open the Account Context Menu by clicking on the user icon that is located at the bottom of your activity-bar (See the picture w/ an arrow below):
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/configHost *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 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
You can now manage your repositories both from the command line and VSCode GUI !