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)
- generate SSH keys, one per account
cd ~/.ssh
ssh-keygen -t rsa -P "" -f account1
ssh-keygen -t rsa -P "" -f account2
Register account1.pub and accuont2.pub to their respective GitHub account: see "Adding a new SSH key to your GitHub account"
Setup ~/.ssh/config
with:
Host account1
HostName github.com
User git
IdentityFile ~/.ssh/account1
Host account2
HostName github.com
User git
IdentityFile ~/.ssh/account2
- 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 >
)
- 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.