•
3 min read
•
Agustinus Nathaniel

How I Manage Multiple Git Accounts on One Machine

https://images.unsplash.com/photo-1618401471353-b98afee0b2eb?auto=format&q=80

If you have both a personal GitHub account and a work Git account, you’ve probably been there: committing to a work repository with your personal email, or vice versa. Switching configs manually is tedious and error-prone.

There’s a better way. Git’s includeIf directive and SSH config let you automatically use the right identity based on the folder or host you’re working with.

The Problem 🔗

Most developers solve this by running git config user.email every time they clone a new repo. But that’s friction. Friction leads to mistakes. And mistakes lead to “Oops, I committed to the company repo with my personal email” - or worse, the other way around.

Solution 1: Folder-Based GitConfig 🔗

This is my preferred approach. It matches the current working directory to determine which Git identity to use.

Step 1: Set Up Your Main Config 🔗

In your global ~/.gitconfig:

[user]
  email = your_personal@email.com
  name = Your Name
  username = your_username

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig_work

[credential]
  helper = manager

Step 2: Create a Work-Specific Config 🔗

In ~/.gitconfig_work:

[user]
  name = Your Name
  email = your_work@company.com
  username = work_username

[core]
  sshCommand = "ssh -i ~/.ssh/id_work"

Now any repository inside ~/work/ automatically uses your work identity and SSH key. No manual switching required.

Solution 2: Host-Based SSH Config 🔗

If you prefer matching by Git host rather than folder, use SSH config:

# Personal GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_personal

# Work GitHub
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_work

When cloning a work repo, use the work host alias:

git clone git@github.com-work:company/repo.git

Which One Should You Use? 🔗

ApproachBest For
GitConfig (includeIf)Monorepos, multiple work folders, or when you want zero mental overhead
SSH ConfigSingle work account per host, or when you want explicit control per clone

I use includeIf because it requires zero thought after setup. I drop a repo in ~/work/ and it just works.

Pro Tips 🔗

  • Generate separate SSH keys for each account using ssh-keygen. Name them clearly (id_personal, id_work).
  • Test your setup with ssh -T git@github.com and ssh -T git@github.com-work.
  • Use a credential manager so you don’t have to type passwords for HTTPS remotes.

References 🔗


This post was originally a quick note in my digital garden. Expanded here after repeatedly sharing it with teammates who had the same problem.