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.
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.
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": "W3Dojo@gmail.com",
"user.name": "jD3V"
},
{
"user.email": "AndrewJay.zck@gmail.com",
"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 thats 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 "urGitHubEmail@foobar.com"
~$ git config --local user.name "urGitHubUsrName"
~$ git config --local user.email "urGitHubEmail@foobar.com"
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.
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!
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 !
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.