5

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?

Worker1432
  • 53
  • 1
  • 6

2 Answers2

7

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
    

Step-by-step, assuming an active shell session in WSL2 (do change the names by something more meaningful to you)

  1. generate SSH keys, one per account
cd ~/.ssh
ssh-keygen -t rsa -P "" -f account1
ssh-keygen -t rsa -P "" -f account2
  1. Register account1.pub and accuont2.pub to their respective GitHub account: see "Adding a new SSH key to your GitHub account"

  2. Setup ~/.ssh/config with:

Host account1
  HostName github.com
  User git
  IdentityFile ~/.ssh/account1
Host account2
  HostName github.com
  User git
  IdentityFile ~/.ssh/account2
  1. Clone your repositories using the right URL (and with it, the right account)
cd
git clone account1:<NameAccount1>/<Repo1>
git clone account1:<NameAccount2>/<Repo2>

(<...> are placeholder values, meant to be replaced: do not keep < and >)

  1. In each repository, set the right commit authroship
cd ~/<repo1>
git config user.name <NameAccount1>
git config user.email <NameAccount1@email.com> # the one registered with GitHub

cd ~/<repo2>
git config user.name <NameAccount2>
git config user.email <NameAccount2@email.com> # the one registered with GitHub

After those steps, each commit will be done with the right GitHub account, and push to the right GitHub repository.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I think this would help! Could you please link either a step by step process? I don't have a mighty lot of technical sophistication and I would really value any help in that direction. thanks in advance! – Worker1432 Oct 16 '21 at 07:21
  • @Worker1432 Sure. I have edited the answer to add a step-by-step process. – VonC Oct 16 '21 at 11:57
  • Do I need to delete my existing SSH keys and local ssh files before proceeding? – Worker1432 Oct 16 '21 at 14:18
  • @Worker1432 Not necessarily, but if this is working, you will need to do some cleanup indeed. – VonC Oct 16 '21 at 14:23
  • you've written multiple lines of code for the same command (for example, in the first step, you create two SSH keys simultaneously). Am I to copy and paste that in the command line as it is, or separately? (and do I need to enter any custom details within the inverted commas, or rename ''account1" and "account2" with any other relevant details?) – Worker1432 Oct 16 '21 at 14:46
  • @Worker1432 You enter each command one at a time. As the answer says: "do change the names by something more meaningful to you" – VonC Oct 16 '21 at 14:57
  • ~/.ssh/config keeps returning an error of "no such file or directory". My account1.pub (and .txt) files are in a directory named ssh1 and similarly for account2.pub saved in .ssh2. But even when I replace the original command with ~/.ssh1/config or ~/.ssh2/config, it shows the same error message. – Worker1432 Oct 16 '21 at 15:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238227/discussion-between-worker1432-and-vonc). – Worker1432 Oct 16 '21 at 15:16
0

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

Tan Su
  • 29
  • 5
  • But that would not be a best practice, would it? Separate keys means gaining access to one key does not give you access to both GitHub account. – VonC Oct 15 '21 at 07:01
  • They share the same public key. But even if others get your public key. They can't use it to login, unless they also get your private key. So I think that's exactly how key works. – Tan Su Oct 15 '21 at 09:51