Skip to content

Instantly share code, notes, and snippets.

@lucas-barake
Last active April 20, 2023 20:41
Show Gist options
  • Select an option

  • Save lucas-barake/9fcfaf35f991a19c3d35bcc0aff356d2 to your computer and use it in GitHub Desktop.

Select an option

Save lucas-barake/9fcfaf35f991a19c3d35bcc0aff356d2 to your computer and use it in GitHub Desktop.
Multiple Git Accounts in Windows

1. Start OpenSSH Authentication Agent Service

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.

2. Generate the SSH Key Pairs

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.

3. Create the Config File

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>

4. Add the generated SSH keys to the corresponding GitHub accounts

Make sure to copy the generated key's .pub. One way to do this is to run:

cat .\<personal>.pub

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

5. Add the keys to the SSH agent

Run the following command:

ssh-add .\<github-username>

Enter the passphrase when asked.

6. Verify authentication

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.

7. Add the corresponding .gitconfig files

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

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

Step 7

Cloning a repository

To clone a repository, run the following command:

git clone git@<added-username>:<owner-username>/<repo-name>.git

For 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment