Stop committing with the wrong email: Multiple Git Configs for GitHub

August 20, 2025

As a developer working on both personal and professional projects, you might find yourself needing to use different SSH keys and email addresses when committing to GitHub. This is especially common when you want to keep your work and personal contributions separate, or when you need to use different SSH keys for different organizations.

In this guide, I'll show you how to set up multiple Git configurations using separate config files, allowing you to automatically use the right SSH key and email based on which directory you're working in.

This guide assumes you sort your git directories by folder. Personally, I keep all my private repos in ~/Projects/perso and all my work repos in ~/Projects/<company_name>. To simplify this post, let's use ~/perso and ~/pro.

Note that I'll be focusing on mac and some commands may vary between OS.

The Problem

By default, Git uses a single global configuration (~/.gitconfig) for all repositories. You might have setup this once when you got your laptop with git config --global user.email ... for instance. This means:

  • All commits use the same email address
  • All SSH connections use the same key
  • You can't easily switch between personal and professional identities (you could configure the git config per repo, but it comes really cumbersome and error prone)

The Solution: Multiple Git Config Files

We'll create separate Git configuration files for different contexts and use Git's includeIf directive to automatically load the right config based on your current directory.

Step 1: Create Your SSH Keys (optional)

If you already have several SSH keys, you can skip this section πŸ™

To generate separate SSH keys for personal and professional use we'll use ed25519 keys (have a look here for details about why). Feel free to use RSA if you like verbosity.

# Personal SSH key
ssh-keygen -t ed25519 -C "your-personal-email@example.com" -f ~/.ssh/id_ed25519_perso

# Professional SSH key
ssh-keygen -t ed25519 -C "your-work-email@company.com" -f ~/.ssh/id_ed25519_pro

When prompted, you can either:

  • Press Enter to create keys without a passphrase (less secure but more convenient)
  • Enter a strong passphrase for additional security (if you do so, I'd recommend to add them to your keychain on mac.)

Step 2: Add Keys to SSH Agent

Add both keys to your SSH agent:

ssh-add ~/.ssh/id_ed25519_perso
ssh-add ~/.ssh/id_ed25519_pro

You can read more about it in the github docs.

Step 3: Configure SSH for Different Hosts

Create or update your ~/.ssh/config file:

# Personal GitHub
Host github-perso
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_perso
    # If you use a passkey, you might need more options like UseKeychain, AddKeysToAgent and IdentityAgent.

# Professional GitHub
Host github-pro
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_pro

Step 4: Set Up Git Config Files

By default Git will use ~/.gitconfig. When you're in a directory that matches the gitdir pattern:

  • ~/perso/ β†’ will load ~/.gitconfig-perso config override.
  • ~/pro/ β†’ will load ~/.gitconfig-pro config override.

Main Git Config (~/.gitconfig)

This is your global configuration that applies to all repositories:

[user]
    name = Your Name
    email = your-personal-email@example.com

[core]
    editor = vim

[init]
    defaultBranch = main

# Include personal config for personal projects
[includeIf "gitdir:~/perso/"]
    path = ~/.gitconfig-perso

# Include professional config for work projects
[includeIf "gitdir:~/pro/"]
    path = ~/.gitconfig-pro

Personal Git Config (~/.gitconfig-perso)

[core]
  sshCommand = "ssh -i ~/.ssh/id_ed25519_perso -F /dev/null"

Professional Git Config (~/.gitconfig-pro)

[core]
  sshCommand = "ssh -i ~/.ssh/id_ed25519_pro -F /dev/null"

[user]
    name = A very profesional name
    email = "my-profesional@email.com"
    signingkey = /Users/<your_user>/.ssh/id_ed25519_pro.pub

Step 5: Add SSH Keys to GitHub

Copy your personal & professional public key:

pbcopy < ~/.ssh/id_ed25519_perso.pub
pbcopy < ~/.ssh/id_ed25519_pro.pub

Add both keys to your GitHub account:

  • Go to GitHub β†’ Settings β†’ SSH and GPG keys
  • Click "New SSH key"
  • Paste each key and give them descriptive names
  • (optional) Add both your keys as Authentication keys and Signing keys. This will help you to sign your commits using your SSH key.

To verify that your commits are authentic, you can sign them using your SSH keys. This provides cryptographic proof that the commit came from you. It also looks nice in the github UI ☺️

Verified commit

Configure Git for SSH Signing

Update your Git configs to use SSH signing:

Main config (~/.gitconfig):

[gpg]
    format = ssh
[commit]
    gpgsign = true

Bonus (hide your email) πŸ₯·

In some cases, you might want to hide your email on Github. To do so, you can head to https://github.com/settings/emails and enable "Keep my email addresses private".

Private email

By using the random email github generates, here 11248623+Lp-Francois@users.noreply.github.com for my account, Github will hide your personal email.

To use it with your git config, put it in this block:

[user]
    name = Francois Le Pape
    email = "11248623+Lp-Francois@users.noreply.github.com" # email found in https://github.com/settings/emails"
    signingkey = /Users/francois/.ssh/id_ed25519_perso.pub

Conclusion

With this setup, you now have a clean separation between your personal and professional Git identities. No more accidentally committing with the wrong email or SSH key!

The beauty of this approach is that it's completely automatic - Git will detect which directory you're in and apply the appropriate configuration. You can focus on coding instead of remembering to switch Git configs manually.

Hope this helps ✨