A guided bash script that walks you through setting up two GitHub accounts (personal + work) on the same Mac — with automatic profile switching based on which directory your repo lives in.
No more committing as the wrong user, no juggling credentials manually.
- Prompts for your personal and work GitHub usernames, emails, and workspace directories
- Generates a separate ed25519 SSH key for each account
- Adds both keys to
~/.ssh/configwith unique host aliases and macOS Keychain integration - Creates
~/.gitconfig-personaland~/.gitconfig-workwith per-account identity and URL rewriting - Adds
includeIfdirectives to your global~/.gitconfigso git picks the right profile by directory - Pauses and guides you through adding each public key to the correct GitHub account
- Tests both SSH connections and reports results
Existing files are backed up with a timestamp before anything is modified. The script is safe to re-run.
- macOS
gitandssh
git clone https://github.com/aldoportillo/github-multi-account-setup.git
cd github-multi-account-setup
./setup.shDirectory-based identity switching
Git's includeIf "gitdir:..." directive conditionally loads a config file based on where the repo lives. The script adds two of these to your global ~/.gitconfig:
[includeIf "gitdir:~/Workspace/personal/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/Workspace/work/"]
path = ~/.gitconfig-workEach profile sets the correct user.name / user.email and rewrites GitHub URLs to use the account-specific SSH host alias.
SSH host aliases
~/.ssh/config maps two aliases to github.com with different identity files:
Host github.com-personal
HostName github.com
IdentityFile ~/.ssh/id_ed25519_personal
Host github.com-work
HostName github.com
IdentityFile ~/.ssh/id_ed25519_work
URL rewriting
Each gitconfig profile rewrites git@github.com: and https://github.com/ to the correct alias automatically, so standard clone URLs just work once the repo is in the right workspace.
After setup, clone into the correct workspace directory:
# Personal repos
cd ~/Workspace/personal
git clone git@github.com-personal:username/repo.git
# Work repos
cd ~/Workspace/work
git clone git@github.com-work:org/repo.gitOnce cloned, git pull, git push, etc. all work normally — no alias needed after the initial clone.
# In a personal repo
cd ~/Workspace/personal/some-repo
git config user.email # → your personal email
# In a work repo
cd ~/Workspace/work/some-repo
git config user.email # → your work email| File | Purpose |
|---|---|
~/.ssh/id_ed25519_personal |
SSH key for personal account |
~/.ssh/id_ed25519_work |
SSH key for work account |
~/.ssh/config |
Host aliases mapping each key to github.com |
~/.gitconfig-personal |
Personal identity + URL rewrite rules |
~/.gitconfig-work |
Work identity + URL rewrite rules |
~/.gitconfig |
Global config updated with includeIf directives |
- Private keys stay on your machine in
~/.ssh/and are never printed or stored anywhere else. - Keys are added to the macOS Keychain so you only unlock them once per login session.
- If a key is compromised, revoke it on GitHub (Settings → SSH keys) and re-run the script to generate a new one.