Home > Software design >  How to switch between two git accounts on same device, in the Terminal (WSL2)?
How to switch between two git accounts on same device, in the Terminal (WSL2)?

Time:10-15

I have created two separate GitHub accounts.

I have also generated two separate SSH keys for them too, using the same device.

When I typed git config user.name in the terminal, the output shows USERNAME2.

I want to work in account 1 right now, so I typed git config --global user.name "USERNAME1 and did the same for the email.

But I think that it hasn't truly switched accounts and just renamed my username or something? The reason I think that is because when I'm trying to create a new repo in account, for ALL git commands, I keep getting a fatal: not a git repository (or any of the parent directories): .git error, which I never got before now, when I had a single account running.

My SSH keys are in two different folders (for account1, it's in .ssh in the home dir, and for account2, it's in .newssh folder in home dir).

What do I do?

CodePudding user response:

I want to work in account 1 right now, so I typed git config --global user.name "USERNAME1"

That has nothing to do with remote Git server authentication.
It only sets local commit authorship.

To use different accounts, you need different remote URLs, each URL set to use the proper private ssh key for remote authentication.

This is done with ~/.ssh/config, as shown here.

Host github2
HostName github.com
User git
IdentityFile /home/me/.newssh/key2

And it supposes:

  • you have git init myProjectFolder, in order to work within ProjectFolder

  • you have set the correct remote URL in the Projectfolder

      git remote add origin github2:<me>/myProject
    

CodePudding user response:

You can use only one ssh key, just add the pub key to both github accounts.

  • Related