Seamlessly Managing Multiple GitHub Accounts on macOS via SSH & includeIf
Original Content Disclaimer: This article shares my personal workflow for managing multiple GitHub accounts. All configurations and code examples are original and created for educational purposes.
Introduction
As developers, it's common to maintain separate GitHub accounts for personal projects and professional work. While this separation helps keep repositories organized, it often introduces a frustrating problem: accidentally committing with the wrong email address or pushing to the wrong GitHub account.
In my setup, I maintain:
- āļøPersonal projects inside
~/<span class="vs-class">Projectsspan> - āļøWork-related repositories inside
~/work
Instead of manually switching Git identities every time I change repositories, I configured Git and SSH to automatically select the correct account based on the repository location.
This article documents the complete setup.
The Problem
Without proper configuration, Git uses a single global identity:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
This creates several issues:
- āļøPersonal commits appear under a work email.
- āļøWork commits appear under a personal email.
- āļøGitHub contribution graphs become inaccurate.
- āļøDevelopers frequently need to modify global Git configuration.
- āļøMultiple GitHub accounts become difficult to manage.
The goal is simple:
| Repository Location | GitHub Account | Email |
| ------------------- | -------------- | ----------------------------------------------------------- |
| ~/<span class="vs-class">Projectsspan> | Personal | yogeshtandan25@gmail.com |
| ~/work | Work | yogesh.work@gmail.com |
Solution Overview
The setup consists of two independent layers:
Layer 1: SSH Authentication
SSH determines which GitHub account is used when cloning, pushing, or pulling repositories.
Layer 2: Git Identity
Git determines which author name and email are attached to commits.
Both layers must be configured correctly.
Step 1: Create Separate SSH Keys
Generate an SSH key for each GitHub account.
Personal Account
ssh-keygen -t ed25519 -<span class="vs-class">Cspan> "yogeshtandan25@gmail.com"
Save as:
~/.ssh/id_ed25519_personal
Work Account
ssh-keygen -t ed25519 -<span class="vs-class">Cspan> "yogesh.work@gmail.com"
Save as:
~/.ssh/id_ed25519_work
Step 2: Add Keys to SSH Agent
Start the SSH agent:
eval "$(ssh-agent -s)"
Add both keys:
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work
Verify:
ssh-add -l
Step 3: Configure SSH Host Aliases
Edit:
~/.ssh/config
Add:
# <span class="vs-class">Personalspan> <span class="vs-class">GitHubspan>
<span class="vs-class">Hostspan> github-personal
<span class="vs-class">HostNamespan> github.com
<span class="vs-class">Userspan> git
<span class="vs-class">IdentityFilespan> ~/.ssh/id_ed25519_personal
<span class="vs-class">IdentitiesOnlyspan> yes
# <span class="vs-class">Workspan> <span class="vs-class">GitHubspan>
<span class="vs-class">Hostspan> github-work
<span class="vs-class">HostNamespan> github.com
<span class="vs-class">Userspan> git
<span class="vs-class">IdentityFilespan> ~/.ssh/id_ed25519_work
<span class="vs-class">IdentitiesOnlyspan> yes
This allows GitHub to recognize each account independently.
Step 4: Configure Automatic Git Identity Switching
Git provides a powerful feature called includeIf.
This feature loads different configuration files depending on the repository path.
Global Configuration
Edit:
~/.gitconfig
[filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[includeIf "gitdir:/Users/mac/Projects/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:/Users/mac/work/"]
path = ~/.gitconfig-work
Notice that there is no global user email.
The identity is determined entirely by repository location.
Step 5: Create Personal Git Configuration
Create:
~/.gitconfig-personal
[user]
name = <span class="vs-class">Yogeshspan> <span class="vs-class">Tandanspan>
email = yogeshtandan25@gmail.com
Step 6: Create Work Git Configuration
Create:
~/.gitconfig-work
[user]
name = <span class="vs-class">Yogeshspan> <span class="vs-class">Tandanspan>
email = yogesh.work@gmail.com
Step 7: Verify Configuration
Navigate into a personal repository:
cd ~/<span class="vs-class">Projectsspan>/my-project
Check:
git config user.email
Expected:
yogeshtandan25@gmail.com
Navigate into a work repository:
cd ~/work/internal-tool
Check:
git config user.email
Expected:
yogesh.work@gmail.com
Verify the Source of Configuration
To determine exactly which configuration file Git is using:
git config --show-origin --get user.email
Example output:
file:/<span class="vs-class">Usersspan>/mac/.gitconfig-work
yogesh.work@gmail.com
This command is extremely useful when debugging configuration issues.
Common Pitfall: Local Repository Overrides
Git follows a hierarchy:
<span class="vs-class">Localspan> <span class="vs-class">Repositoryspan> <span class="vs-class">Configspan>
ā
<span class="vs-class">Includedspan> <span class="vs-class">Configspan>
ā
<span class="vs-class">Globalspan> <span class="vs-class">Configspan>
ā
<span class="vs-class">Systemspan> <span class="vs-class">Configspan>
If a repository already contains:
git config --local user.email
that value overrides everything else.
Check:
git config --local --list
Remove existing overrides:
git config --unset user.name
git config --unset user.email
Cloning Repositories Correctly
SSH aliases only work if they are used in repository URLs.
Personal Repository
git clone git@github-personal:username/project.git
Work Repository
git clone git@github-work:organization/project.git
Avoid:
git clone git@github.com:user/project.git
because this bypasses the SSH alias configuration.
Final Folder Structure
/<span class="vs-class">Usersspan>/mac
ā
āāā <span class="vs-class">Projectsspan>
ā āāā portfolio
ā āāā side-project
ā āāā experiments
ā
āāā work
āāā internal-tools
āāā client-projects
āāā production-apps
With this structure, Git automatically applies the correct identity and SSH key without any manual intervention.
Conclusion
Managing multiple GitHub accounts does not require constantly switching credentials or editing Git configuration files.
By combining:
- āļøSeparate SSH keys
- āļøSSH host aliases
- āļøConditional Git configuration using
includeIf
it is possible to create a clean and fully automated workflow.
In my setup, repositories under ~/<span class="vs-class">Projectsspan> automatically use my personal GitHub account, while repositories under ~/work use my work account. Once configured, the process becomes completely transparent, reducing mistakes and keeping commit history accurate across all projects.