Blog

How to run two GitHub accounts in the same laptop without friction

Diario del capitán, fecha estelar d321.y42/AB

Man juggling

A couple of weeks ago I ran into a problem that anyone who has run a client-services business for more than a few years will recognise immediately. I'd just been set up with a new GitHub account, and everything was working within minutes, but my access to a client's repos was still tied to my old account, and moving it over meant opening a ticket and waiting. I didn't want my work blocked on a queue for something this small.

I don’t like sitting with my arms folded, so I decided that I would keep both accounts: the old one for that client and the new one for everything else, and make the switch invisible enough so that I'd never have to think about which identity I was using on a given day.

That's the part I actually want to write about, because it's a small example of something we care about a lot at MarsBased: keeping client work cleanly separated from everything else, by default, without relying on anyone remembering to be careful.

Two problems, not one

There are actually two separate things going on when you work across multiple GitHub identities on one machine:

  • Identity: whose name and email end up on a commit.
  • Authentication: which SSH key gets used to talk to GitHub in the first place.

I'd seen people solve these together before and end up with something fragile. Solved separately, each one is a few lines of config.

Identity, routed by folder

I keep client work under predictable folder structures, and that structure is what does the routing. Git's includeIf lets you say "load a different config depending on which folder I'm in":

# ~/.gitconfig
# Default identity for everything under ~/work/
[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig.default

# Override for one specific client, matched by folder.
# gitdir/i makes it case-insensitive; the trailing slash matters.
[includeIf "gitdir/i:~/work/client-x/"]
    path = ~/work/.gitconfig.client-x

Order matters here. The more specific block has to come after the general one, so anything inside ~/work/client-x/ picks up the override instead of the default.

Authentication, forced by an SSH flag I almost skipped

Two SSH keys, one per account:

# ~/.ssh/config
Host github.com
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519_default
    IdentitiesOnly yes

Host github-client-x
    Hostname github.com
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_ed25519_client_x
    IdentitiesOnly yes

IdentityFile is obvious. IdentitiesOnly yes is the line that actually matters, and it's the one I almost left out. Without it, SSH will happily try every key sitting in your agent until one of them is accepted, which means GitHub can silently authenticate you as the wrong account. That's not a hypothetical edge case, it's the standard way this whole setup breaks.

Making it invisible

None of this is worth doing if I still had to remember a special clone URL for client repos. So the last piece was telling Git to rewrite URLs on the fly:

# ~/.gitconfig
[url "git@github-client-x:client-x-org/"]
    insteadOf = https://github.com/client-x-org/
    insteadOf = git@github.com:client-x-org/

Now it doesn't matter whether I copy an HTTPS or SSH URL straight from GitHub's UI. Anything pointing at that client's org gets transparently rewritten to use the alias, and therefore the right key. I clone it the normal way and the correct account authenticates behind the scenes.

Working in the client's folder, both the commit identity and the SSH key switch to the client's, and everything else keeps using the default email and default key.

I shared this internally and a couple of things came back that I liked more than my original setup. One teammate had already built something close to it to separate a personal learning account from client work, and hit the exact same key-collision problem before landing on the same fix. Another mentioned direnv as a possible way to simplify the whole thing further, using it to manage per-project environment variables rather than juggling config files by hand. He hadn't switched over yet because what he had was already working, which, honestly, is a perfectly good reason not to touch something.

I don't think there's one correct way to do this. What I do think is that "which account am I even using right now" shouldn't be a question you have to hold in your head while working on client code. The setup above isn't clever, it's just a few config files doing the remembering for you. For an agency working across as many client codebases as we do, that's not a nice-to-have, it's part of how we keep things separated properly in the unlikely event where we have to switch between two GitHub accounts.

Compartir este post

Artículos relacionados

Microchips

Iterating on pull requests - don't squash commits!

Don't squash commits when iterating on pull requests. It destroys context and makes code reviews harder. Here's why keeping your Git commit history matters.

Leer el artículo: Iterating on pull requests - don't squash commits!
Docker

Always use a .dockerignore in your projects

A missing .dockerignore slows down deployments and risks exposing sensitive data. Learn why this small but critical file is as essential as .gitignore.

Leer el artículo: Always use a .dockerignore in your projects
AI-Augmented development

Standardising innovation: Our internal guide to AI-augmented development

How we integrate agents like Claude and Copilot into our workflow using a rigorous Research, Plan, and Implement framework to ensure speed without sacrificing architectural excellence.

Leer el artículo: Standardising innovation: Our internal guide to AI-augmented development