Start by enabling OpenSSH Authentication Agent.
Open Services, right click OpenSSH Authentication Agent, click Properties, change Startup type to Automatic, hit Apply, then right click once again, and click Start.
Go over to the .ssh folder in your user, and type the following command in PowerShell:
ssh-keygen -t rsa -C "[email protected]" -f "github-username"-t rsa: Specifies the type of key to create as an RSA key.
-C "[email protected]": Specifies an optional comment.
-f "github-username": Specifies the filename of the key.
Repeat these for all of the accounts you want to add.
cd into the .ssh folder and run the following command to create the configuration file and start writing on it directly within the terminal:
bash -c "nano config"Add the following lines to the file, making sure to replace the placeholder text with your own:
Host <personal>
HostName github.com
User git
IdentityFile ~/.ssh/<personal>
Host <work>
HostName github.com
User git
IdentityFile ~/.ssh/<work>Make sure to copy the generated key's .pub. One way to do this is to run:
cat .\<personal>.pubSelect the key with your cursor and copy the content.
Go to GitHub > Settings > SSH and GPG keys > New SSH key.
Add a title (for example, Windows 11) and paste the generated key.
Run the following command:
ssh-add .\<github-username>Enter the passphrase when asked.
Run the following command:
ssh -Tv <github-username>It should output
Hi github-username! You've successfully authenticated, but GitHub does not provide shell access.
This means that you've configured your account(s) properly through SSH.
Suppose you have two different GitHub accounts, one for work and one for personal.
You will have a different .gitconfig file for each root folder where your projects will be stored in respectively:
Personal ~/personal/.gitconfig:
[user]
name = Personal
email = [email protected]Work ~work/.gitconfig:
[user]
name = Work
email = [email protected]You can add these via nano once you cd to the corresponding folders:
bash -c "nano .gitconfig"Now edit the global configuration file to point to these local configurations. Go to the root of your user and run the following:
bash -c "nano .gitconfig"You must include the following:
[includeIf "gitdir:C:/Users/<user>/src/<personal>"]
path = C:/Users/<user>/src/<personal>/.gitconfig
[includeIf "gitdir:C:/Users/<user>/src/<work>"]
path = C:/Users/<user>/src/<work>/.gitconfig
[user]
name = <personal>
email = <personal>@email.com
[core]
sshCommand = C:/Windows/System32/OpenSSH/ssh.exeYou must add the [user] config for it to work. I'd default to using your personal information, though it won't matter since we override the account info per folder.
To clone a repository, run the following command:
git clone git@<added-username>:<owner-username>/<repo-name>.gitFor example, if one of my usernames is personal and I want to clone the repository my-repo from the owner my-owner, I would run:
git clone git@personal:my-owner/my-repo.git