Working with multiple git accounts

Overview

Working with multiple Git accounts can often lead to authentication conflicts – especially when you’re collaborating across different GitHub organizations or juggling between platforms like GitHub and Azure DevOps.

A common scenario involves Git clone via HTTP failing to authenticate correctly (in my case it doesn’t work with GitHub organization), or being cumbersome to switch between different user credentials (especially when working with Azure DevOps).

There are several approaches to mitigate these issues:

  • Passing credentials in the URL.
  • Using Git Credential Manager. You pass the username in the URL and let Git Credential Manager handle password/token prompts and storage.
  • Using SSH.

From my experience, SSH tends to be more reliable and avoids the constant prompts and switching required by HTTP-based auth. Once set up properly, SSH enables:

  • Password-less access (after initial key setup)
  • Persistent sessions across terminal sessions

However, SSH authentication is domain-based. For example, GitHub uses a fixed domain for SSH “git@github.com”.

This means that regardless of your GitHub username, the SSH connection string remains the same. This becomes problematic when using multiple accounts on the same domain.

To work around this, you can configure custom host aliases in your ~/.ssh/config file. Each alias maps to github.com but uses a different identity file and user.

For example:

# GitHub Account 1
Host github.com-hung-doan
  HostName github.com
  User git
  IdentityFile ~/.ssh/hung-doan/id_rsa

# GitHub Account 2
Host github.com-harry-doan
  HostName github.com
  User git
  IdentityFile ~/.ssh/harry-doan/id_rsa

Then:

  • When I use “git clone git@github.com-hung-doan/…” it will resolve “github.com” using the configuration for “hung-doan” account.
  • When I use “git clone git@github.com-harry-doan/…” it will resolve “github.com” using the configuration for “harry-doan” account.

Detailed steps

Step 1: Open PowerShell or Terminal

Press Win + X and select Windows Terminal or PowerShell (preferably run as Administrator).

Step 2: Create your custom folder (if not already created)

mkdir "$HOME\.ssh\github\your-username"

Step 3: Generate SSH Key

Run the following command to generate an SSH key with a custom name (replace your email address):

ssh-keygen -t ed25519 -C "your-email@example.com" -f "$HOME\.ssh\github\your-username"

or

ssh-keygen -t rsa-sha2-512 -C "your-email@example.com" -f "$HOME\.ssh\github\your-username"
  • When prompted, you can set a passphrase or leave it empty by pressing Enter twice.

Step 4: Configure SSH to use the custom key automatically

Create or edit the SSH config file at:

%USERPROFILE%\.ssh\config

The file content (using Notepad, VS Code, or any editor):

Host github.com-your-username
   HostName github.com
   User your-username
   IdentityFile c:/Users/your-username/.ssh/github/your-username/id_ed25519
   IdentitiesOnly yes

Example:

# GitHub account: hung-doan
Host github.com-hungdoan
HostName github.com
User git
IdentityFile c:/Users/hungdoan/.ssh/github/hung-doan
IdentitiesOnly yes
# GitHub account: harry-doan
Host github.com-harry-doan
HostName github.com
User git
IdentityFile c:/Users/hungdoan/.ssh/github/harry-doan
IdentitiesOnly yes

Note 1: “User git” – why use git as the User?
When connecting to GitHub via SSH, the username is always git, irrespective of your actual GitHub username. GitHub identifies your user account from your SSH key.

Notes 2: SSH over HTTPS

It’s possible that there are security policies that block the port 22 of the SSH, then we can consider the SSH over HTTPS on port 443

# GitHub account: hung-doan
Host github.com-hungdoan
HostName ssh.github.com
Port 443
...

Step 5: Add your SSH public key to GitHub

  • Display your SSH public key using:
Get-Content "$HOME\.ssh\github\hung-doan\id_ed25519.pub"
  • Copy the displayed key.
  • Go to GitHub, navigate to your account settings:
    • Settings -> SSH and GPG keys → New SSH key
    • Paste your key, add a title, and save.

Step 6: Verify your SSH configuration (optional)

Using this command to verify:

sing this command to verify:

ssh -T git@<hostname>

Example:

ssh -T git@github.com-hung-doan

We should see a success message confirming the identity associated with your accounts.

Step 7: Cloning or interacting with ssh repositories

To clone or interact with a repository using a specific SSH key/account, use this syntax:

git clone git@<hostname>:<path>/<to-your-repo>.git

For example:

git clone git@github.com-hung-doan:<path>/<to-your-repo>.git
git clone git@github.com-harry-doan:<path>/<to-your-repo>.git

This will automatically select the correct identity (IdentityFile) for the given host alias.

References

[1] 8 steps to manage multiple GitHub accounts | GitGuardian Blog

[2] Using SSH over the HTTPS port – GitHub Docs

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I’m Hung

Welcome to my personal space. This is a small corner where I share ideas and things I find interesting,..

Let’s connect

Don’t hesitate to reach out if there’s anything we can discuss, or simply to connect :)