← Back to Blog

How to Run Multiple GitHub Accounts on One Machine (Mac)

SSH keys, conditional gitconfig, automatic URL rewriting; the complete setup for running personal and work GitHub accounts on one Mac without ever mixing up identities again.

Developer Tools7 min readAuthor: Kukil Kashyap Borgohain
Managing multiple GitHub accounts on a single machine with SSH

Why I Wrote This Article?

Setting up multiple GitHub accounts on one machine should be a solved problem by now. It isn't! Well, atleast for me Every time I do it - new device, new job, new side project - I make the same mistakes. You'd think after years of this, the steps would be second nature. They're not.

So I wrote this down. Once and for all. This guide covers SSH key setup, SSH config, automatic identity switching and a few other life saving tricks. The URL rewriting trick that saves you from manually editing clone URLs is one of them. If you feel similar, this guide is for you. I hope it helps you as much as it has helped me.

Note: This guide is Mac-focused. I'll write a separate post for Windows later.


Prerequisites (The Obvious Stuff)

Before you start, make sure you have:

  • Git installed (git --version to check)
  • Two GitHub accounts (personal + work) already created
  • Terminal access (Terminal.app, iTerm2, Warp, etc.)
  • Basic familiarity with the command line

The Problem

If you're using two GitHub accounts without proper SSH configuration, Git defaults to a single identity. That means:

  • Commits get attributed to the wrong account
  • Push/pull operations fail with permission errors
  • You end up in a cycle of re-authenticating every time you switch contexts

The clean solution involves three things:

  1. Separate SSH keys : one per GitHub account
  2. SSH config aliases : so SSH picks the right key automatically
  3. Automatic Git identity : so you never forget to set user.name / user.email

Once configured, everything routes automatically. No switching, no remembering.


Step 1: Generate SSH Keys

Your Mac likely already has a default SSH key (id_ed25519 or id_rsa) : this is the key generated when you first set up SSH. We'll treat that as your work key and generate a new one for personal use.

bash
1ssh-keygen -t ed25519 -C "your_personal_email@example.com"

When prompted for the file location, save it with a distinct name:

bash
1/Users/yourusername/.ssh/id_ed25519_personal

After this, you should have two key pairs in ~/.ssh/:

FilePurpose
id_ed25519Work account (default)
id_ed25519.pubWork public key
id_ed25519_personalPersonal account
id_ed25519_personal.pubPersonal public key

Step 2: Add Keys to SSH Agent

Load both keys into the SSH agent so macOS remembers them across sessions:

bash
1eval "$(ssh-agent -s)"
2ssh-add --apple-use-keychain ~/.ssh/id_ed25519
3ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal

The --apple-use-keychain flag stores the passphrase in macOS Keychain, so you don't have to type it every time.


Step 3: Configure SSH

Edit (or create) ~/.ssh/config:

bash
1nano ~/.ssh/config

Add the following:

bash
1# Work GitHub
2Host github-work
3  HostName github.com
4  User git
5  IdentityFile ~/.ssh/id_ed25519
6  IdentitiesOnly yes
7  UseKeychain yes
8  AddKeysToAgent yes
9
10# Personal GitHub
11Host github-personal
12  HostName github.com
13  User git
14  IdentityFile ~/.ssh/id_ed25519_personal
15  IdentitiesOnly yes
16  UseKeychain yes
17  AddKeysToAgent yes

Important: IdentitiesOnly yes is critical. Without it, the SSH agent may offer all loaded keys to GitHub, and GitHub accepts whichever key matches first : which could be the wrong account. This flag forces SSH to use only the specified IdentityFile.


Step 4: Add Public Keys to GitHub

Copy each public key:

bash
1# Personal key
2cat ~/.ssh/id_ed25519_personal.pub | pbcopy
3
4# Work key (if not already added)
5cat ~/.ssh/id_ed25519.pub | pbcopy

Then for each account:

  1. Go to GitHub >> Settings >> SSH and GPG keys >> New SSH key
  2. Name: Anything you like.
  3. Select Authentication Key
  4. Paste the corresponding public key and save

Step 5: Automatic Identity with Conditional Includes

This is the part I wish I'd set up from the start. Instead of manually running git config user.email in every repo (and inevitably forgetting), you can tell Git to automatically set your identity based on the directory.

First, organize your repos into separate directories:

bash
1~/Repositories/Personal/    # All personal repos go here
2~/Repositories/Work/        # All work repos go here

The global ~/.gitconfig keeps the work identity as default and conditionally overrides it for personal repos:

ini
1[user]
2    name = username-1
3    email = [EMAIL_ADDRESS_1]
4
5[includeIf "gitdir:~/Repositories/Personal/"]
6    path = ~/.gitconfig-personal

Create ~/.gitconfig-personal:

ini
1[user]
2    name = username-2
3    email = [EMAIL_ADDRESS_2]

Now any repo inside ~/Repositories/Personal/ automatically uses my personal identity. Everything else defaults to the company identity. No more forgotten git config commands. This would have saved me from that embarrassing week of pushing to a company repo with personal email.


Step 6: Auto-Rewrite Clone URLs

Here's a trick that saves you from manually editing clone URLs every time. When you copy a clone URL from GitHub, it looks like:

bash
1git@github.com:kXborg/tinyML.git

But with our SSH config, you need to change github.com to github-personal or github-work. Doing this manually every time is tedious and easy to forget.

The fix : add URL rewrite rules using insteadOf. In my case, the global ~/.gitconfig defaults to the work SSH alias:

ini
1[url "git@github-work:"]
2    insteadOf = git@github.com:

And in ~/.gitconfig-personal (which only applies inside ~/Repositories/Personal/), override the rewrite to use the personal alias:

ini
1[user]
2    name = kXborg
3    email = kukilp213@gmail.com
4
5[url "git@github-personal:"]
6    insteadOf = git@github.com:

Now when you clone inside ~/Repositories/Personal/:

bash
1git clone git@github.com:kXborg/tinyML.git
2# >> Git rewrites to git@github-personal:kXborg/tinyML.git ✅

And when you clone anywhere else (work by default):

bash
1git clone git@github.com:workID/some-project.git
2# >> Git rewrites to git@github-work:workID/some-project.git ✅

No manual URL editing. Fully automatic.


Step 7: Clone Repos Using the Right Host

With URL rewriting from Step 6, you can just clone normally. Git handles everything behind the scenes:

bash
1# Inside ~/Repositories/Personal/
2git clone git@github.com:kXborg/tinyML.git
3# >>  Rewrites to git@github-personal:username-2/tinyML.git ✅
4
5# Inside ~/Repositories/Work/
6git clone git@github.com:workID/some-project.git
7# >>  Rewrites to git@github-work:workID/some-project.git ✅

If you prefer to be explicit (or haven't set up URL rewriting), use the SSH alias directly:

bash
1git clone git@github-personal:kXborg/tinyML.git
2git clone git@github-work:workID/some-project.git

For existing repos that were cloned with github.com, update the remote:

bash
1git remote set-url origin git@github-personal:kXborg/orbital.git

Verification

Test both SSH connections:

bash
1ssh -T git@github-work
2ssh -T git@github-personal

Expected output for each:

Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

Each should show the corresponding GitHub username. If you see the wrong username, double-check your SSH config's IdentityFile paths and make sure IdentitiesOnly yes is set.

Verify your Git identity in a repo:

bash
1cd ~/Downloads/Repositories/Work/some-repo
2git config user.email
3# Should output: 'email_1'
4
5cd ~/Downloads/Repositories/Personal/orbital
6git config user.email
7# Should output: 'email_2'

Common Pitfalls

ProblemCauseFix
Commits show wrong emailForgot git config user.email in repoUse conditional includes (Step 5)
Permission denied (publickey)SSH using wrong keyAdd IdentitiesOnly yes to SSH config
Clone URL requires manual editingUsing github.com instead of aliasUse URL rewriting (Step 6)
Keychain keeps asking for passphraseMissing UseKeychain / AddKeysToAgentAdd both to SSH config
Cloned with HTTPS instead of SSHGitHub defaults to HTTPS URLUse the SSH tab when copying clone URL

Quick Reference Commands Table

bash
1# Generate a new SSH key
2ssh-keygen -t ed25519 -C "email@example.com"
3
4# Add key to SSH agent (macOS)
5ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
6
7# Test SSH connection
8ssh -T git@github-personal
9
10# Check current Git identity
11git config user.name && git config user.email
12
13# Update remote URL for existing repo
14git remote set-url origin git@github-personal:user/repo.git

Final Thoughts

Managing multiple GitHub accounts sounds like a 5 minute task, but the devil is in the details : wrong identities, permission errors, and clone URLs that need manual editing. With SSH aliases, conditional gitconfig includes, and URL rewriting, the workflow becomes truly hands-off.

Set it up once. Never think about it again.

If the article helped you in some way, consider giving it a like. This will mean a lot to me. You can download the code related to the post using the download button below.

If you see any bug, have a question for me, or would like to provide feedback, please drop a comment below.