Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I log in two different github account in vscode?

I want to login to two different GitHub accounts in V.S. Code to two GitHub accounts. The reason I want to do this is because I have a personal account, and another that's a company account. I want to be able to push changes I make to repositories the belong to either account, so I use VS Code for my work-projects, and my personal-projects.

If it is not possible to login to two different accounts, then is their a way to easily log out of one account, and then log into another account? I am using the Windows Linux-VM: WSL. My WSL distro is Ubuntu.

like image 429
Yingqi Avatar asked Jun 28 '20 16:06

Yingqi


People also ask

Can I connect 2 GitHub accounts?

If you have separate accounts for work and personal use, you can merge the accounts. Tip: We recommend using only one personal account to manage both personal and professional repositories. Warning: Organization and repository access permissions aren't transferable between accounts.

How do I change my GitHub account in Visual Studio?

If you're not already signed in to Visual Studio with a different account, select the Sign in link in the upper-right corner of the Visual Studio environment. You can also open the Account Settings dialog by going to File > Account Settings.... Then, follow the instructions above to add your GitHub account.


1 Answers

Edit #1: 2021|JUNE|06th @ 12:39am (PST)
Edit #2: 2021|JULY|15th @ 4:46:31 (UTC/GMT)


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.






USE TWO GITHUB ACCOUNTS IN ONE V.S. CODE EDITOR


THE SHORT ANSWER is

"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."



The Long Answer:


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.


STEP #1


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).

NOTE: It is fine if the worktree command doesn't work, that just means you just don't currently have a worktree .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 "[email protected]"

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

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


STEP #2: Extensions


Acquire the following extension from the V.S. Code marketplace.

  • Git-Autoconfig

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.

Git-Autoconfig will automatically set credentials for each new project you clone, or create, however, you need to make sure that it is already configured in your current project. You will individually set an account username and email for each project. To set the git credentials for Git-Autoconfig hit the F1 key to open the Quick-input Menu, and type "git autoconfig set". Select: Set Config.

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": "[email protected]",
              "user.name": "DevJay"
          },
          {
              "user.email": "[email protected]",
              "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."_



You can also use the Git-Autoconfig extension to see what account the project is set to by using Git-Autoconfig quick input command: Get Config, if it doesn't display the right configuration for the right account in the notifications toast pop-up, then use the Get Auto Config _set config command from the quick input, as explained above to configure the project to have the correct account credentials.


As stated above you should already have the - GitHub Pull Requests and Issues installed and working,

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.


STEP #3: Bread & Butter

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.

The tool is called Visual Studio Code's Account Context Menu

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):




Account Context Menu Location




enter image description here enter image description here

like image 91
j D3V Avatar answered Sep 23 '22 13:09

j D3V