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.
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:
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.
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:
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.
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
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.~/.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
~/.gitconfig-perso)[core]
sshCommand = "ssh -i ~/.ssh/id_ed25519_perso -F /dev/null"
~/.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
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:
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 βΊοΈ

Update your Git configs to use SSH signing:
Main config (~/.gitconfig):
[gpg]
format = ssh
[commit]
gpgsign = true
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".

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
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 β¨