BLOG POST

How to Configure Multiple GitHub Accounts on Windows 11.

Dexter Astorga October 15, 2023
Photo created using Lucidchart.

I have been using git for months now and it’s been very useful in version controlling my projects. I created a GitHub Account then used my cmd to communicate to the account. Working with Git and GitHub has been very smooth not until I created another GitHub Account for my freelance projects. It says that Git is not permitting me to make changes to this new account because I do not have permission to do so.

This is where I get into digging for solutions and found Truth Seekers YouTube Video which tackles exactly what I needed. I decided I want to write about it just to share another perspective on how I managed to configure two GitHub Accounts in my machine. For context, I have a personal GitHub account which is logged in to my Chrome Browser and another secondary account logged in to my Opera Browser I use in freelancing.

Goals of this article:

  1. 1. Tackle how to use SSH to manage multiple accounts.
  2. 2. Talk about the format of the configuration file git will use to tell apart from the different accounts.
  3. 3. Test the connection of each account.

A. Key Generation.

SSH is an encryption that, when used with Git and GitHub, allows us to transfer data securely from a local machine to a remote repository. SSH is amazing; it is an abbreviation for Secure Shell. We need a key pair for this to work: a public key and a private key. The public key will be added to a GitHub account, and then the private key will be stored on our local machine. Once a connection is made, the machine will check if the private key and the public key match. If they do, we can proceed with the data transfer.

To do this, first cd to C:\\Users\Dexter.
(Note: I will use my computer for these examples, you just need to change your path according to your machine and you should be good).

cd C:\\Users\Dexter

ssh-keygen -t rsa -C “testEmail1@gmail.com“


  1. -t is the type of encryption
  2. -C is the comment, we want to comment the GitHub Account we want to connect to.

Running these two commands will create a private-public key pair but first, you need to specify which folder these files will be saved. It’s going to suggest

C:\\Users\Dexter/.ssh/id_rsa

but be cautious because it will override an existing id_rsa file. This time though, we will let this override the file. Next, we will set a passphrase for increased security. This is not mandatory you can set it as blank for no passphrase. What’s the difference? Every time you perform git push it will prompt you to enter your passphrase, it will be annoying in the long run but it’s still your call. For me, I want extra protection so I will set mine to

************************

After this, your keys as testEmail1@gmail.com is now saved in the .ssh folder.

  1. id_rsa -> private key
  2. id_rsa.pub -> public key

B. Add an SSH Key on testEmail1@gmail.com GitHub Account.

GitHub > Settings > SSH and GPG Keys > new SSH Key

Title

*name this the machine you are using to access github*

Key

*this going to be the text you’re going to copy from id_rsa.pub*


C. Add the Key to SSH Agent

You need to run this command to activate ssh-agent. Activating ssh-agent will let you run the ssh-add command.

start-ssh-agent

You can now add the private key by running:

ssh-add C:\\Users\Dexter/.ssh/id_rsa

A message should be displayed telling you that your identity as testEmail1@gmail.com has been added.


D. Testing First Account.

  1. i. Create a Repo in GitHub.
  2. ii. Create a directory in your local machine.
  3. iii. Configure the directory.

git init

git config –global user.name “yourUsername”

git config –global user.email yourEmail

git config –global init.defaultBranch main

git add –all

git commit -m “Initial Commit.”


  1. iv. Go back to GitHub then copy the SSH URL. Make sure to select SSH instead of HTTPS URL.
  2. v. In the terminal, push the directory to GitHub.

git remote add origin githubSshUrl

git push -u origin main

*enter passphrase*


If this does not work, jump to step G. If this works, you’re now ready for the second account. We will just repeat most of the steps earlier.


E. Generate Key.

ssh-keygen -t rsa -C “secondTest@gmail.com“

Then, create teh other id_rsa file but instead of id_rsa, we will name it id_rsa_secondary. I am adding a passphrase for this account as well.

C:\\Users\Dexter/.ssh/id_rsa_secondary

**********

After this, your keys as secondTest@gmail.com is now saved in the .ssh folder.

  1. id_rsa_secondary -> private key
  2. id_rsa_secondary.pub -> public key

F. Add a New SSH Key in secondTest@gmail.com GitHub Account.

GitHub > Settings > SSH and GPG Keys > new SSH Key

Title

*name this the machine you are using to access github*

Key

*this going to be the text you’re going to copy from id_rsa_secondary.pub*


G. Add Key to ssh-agent.

If the prompt says: cannot connect to agent when you run the ssh-add command, follow these steps. It’s from this stack overflow post.

  1. Search for “Services” in the start menu.
  2. Find the “Open SSH Authentication Agent” then double click.
  3. Change th StartUp Type from disabled to Automatic Delayed Start.
  4. To confirm that the top-listed path is System32, open cmd then type “where ssh”

You should now be able to use ssh-agent. If not, try restarting the PC and then reopen cmd. Now, run the command:

ssh-add C:\\Users\Dexter/.ssh/id_rsa_secondary

The identity as secondTest@gmail.com should now be added.

Now that we have two accounts registered, the computer will be confused when using GitHub, so we need to create a configuration file.


H. Create a Configuration File.

The file name is “config” without any extension. It must be created inside the .ssh folder. This is the format:

# MAIN ACCOUNT

Host github.com

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_rsa


# SECONDARY ACCOUNT

Host github-secondary

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_rsa_secondary

By now, your directory should look like this:

Now, the computer will be able to tell apart which has access to which account. You should now be able to push and pull repositories on both accounts but with some minor differences.


I. Test the Second GitHub Account.

  1. i. Create a Repo in GitHub.
  2. ii. Create a directory in your local machine.
  3. iii. Configure the directory.

git init

git config –global user.name “yourUsername”

git config –global user.email yourEmail

git config –global init.defaultBranch main

git add –all

git commit -m “Initial Commit.”


  1. iv. Go back to GitHub then copy the SSH URL. Make sure to select SSH instead of HTTPS URL. Here, instead of github.com, you will change it to github-secondary. Example, instead of: dexdev@github.com:testrepo.git, you will change it to: dexdev@github-secondary:testrepo.git
  2. v. In the terminal, push the directory to GitHub.

git remote add origin githubSshUrl

git push -u origin main

*enter passphrase*

Congratulations! Evertything should work fine now.

In summary, using SSH to set up multiple GitHub accounts on Windows 11 allows for safe and smooth sharing of data between your computer and GitHub. Learning how to generate key pairs and use SSH keys helps you manage different accounts more easily. By following the steps outlined, you can handle multiple accounts better, work more smoothly with others, and feel more confident about controlling different versions of your projects.