Set Up Multiple SSH Keys for Git on Windows

Hafiz Muhammad Anwar
Blocship
4 min readOct 30, 2023

--

Introduction: In software development, it’s not uncommon to have multiple GitHub accounts. Managing these accounts efficiently requires setting up separate SSH keys and configurations. In this guide, we’ll talk about the process of creating and configuring SSH keys for two distinct GitHub accounts, enabling you to work seamlessly across both personal and professional repositories.

1. Generate SSH Keys

Creating SSH Key for Personal GitHub Account (Anwar-personal)

To begin, generate an SSH key for your personal GitHub account. Replace ‘anwar@gmail.com’ with your email and choose a unique file name.

ssh-keygen -t rsa -C "anwar@gmail.com" -f "anwar-personal"

Creating SSH Key for Office GitHub Account (Anwar-office)

For your professional GitHub account, create a separate SSH key. Replace ‘anwar@company.com’ with your work email and choose a unique file name.

ssh-keygen -t rsa -C "anwar@company.com" -f "anwar-office"

2. Add SSH Keys to GitHub Profiles

To associate your SSH keys with your GitHub accounts, you’ll need to add them to your GitHub profiles. This ensures you can authenticate with your accounts when interacting with remote repositories.

3. Create a Config File

To compare between your personal and professional SSH keys, create a config file in your .ssh folder. This file will specify the host names and associated SSH keys for each GitHub account.

Step 1: Navigate to the .ssh Folder

Begin by opening your terminal and accessing your .ssh folder. If you don’t have a config file in place, you can create one by following these simple steps.

firstly, navigate to the .ssh folder through the terminal then run this command

notepad config

After running the ‘notepad config’ command, you’ll be prompted to create a new config file. Click ‘Yes’ to proceed.

Step 2: Configuration Setup

Now, it’s time to define the host names and associate them with the respective SSH keys for each of your GitHub accounts. Let’s create an example configuration for both your personal and professional accounts:

# Configuration for the Anwar-personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/anwar-personal

# Configuration for the Anwar-office account
Host github.com-office
HostName github.com
User git
IdentityFile ~/.ssh/anwar-office

You create a hostname like this GitHub.com-office (github.com{-yourOwnName})

anwar-personal/anwar-office file(We created this earlier when we created ssh keys in the “Creating SSH Key” section)

Replace the placeholders(github.com-personal and ~/.ssh/anwar-personal) with your account information, ensuring that the ‘Host’ lines provide unique aliases for each account. This setup will enable you to seamlessly switch between your personal and professional GitHub account

Step 3: Save and Close

Once you’ve pasted the configuration into the notepad file, save your changes and close the file.

Step 4: Terminal Commands

Return to your terminal and execute the following commands to finalize the process:

copy config.txt config
del config.txt

These commands will create the config file and remove the temporary config.txt file. If you already have a config file, you can open it with notepad and paste the new configuration, adjusting the information to match your accounts and aliases.

The Host lines set up unique aliases for each account, making it easy to switch between them.

4. Testing SSH Connectivity

Before proceeding, it’s essential to test your SSH setup to ensure everything functions correctly. Use the following commands to check your connections:

Testing the Office Account SSH Connection

ssh -T git@github.com-office

Testing the Personal Account SSH Connection

ssh -T git@github.com-personal

5. Clone Repositories

When cloning repositories, be sure to specify the hostname corresponding to the GitHub account you want to use. Here’s how to clone repositories for both personal and office accounts:

Cloning a Personal Repository

git clone git@github.com-personal:<repository-url>.git

Cloning an Office Repository

git clone git@github.com-office:<repository-url>.git

6. Configure Local Git User Information

To ensure that your commits are correctly attributed to the appropriate GitHub account, set your local Git user information for each repository:

# Set your email
git config user.email "anwar@gmail.com" (for personal/office)

# Set your username:
git config user.name "anwar-siddiqi" (for personal/office)

7. Checking Local Git Configuration

It’s essential to verify that your local Git configuration is correctly set up for each repository. To check the local Git configuration, use the following command:

git config --local --list

This command will display the configuration details specific to the repository you’re currently working on. Ensure that the email and username are correctly set to match the respective GitHub account you’re using.

By checking your local Git configuration, you can be confident that your commits will be added to the correct GitHub account, whether it’s your personal or professional one. With this additional step, you’ll have clarity when working with multiple GitHub accounts on a single machine.

By following these steps, you can manage multiple GitHub accounts and seamlessly work on both personal and professional repositories without any confusion.

--

--

Hafiz Muhammad Anwar
Blocship
Writer for

Hi, I'm a Flutter mobile application developer with 2 years of professional experience.