Managing multiple GitHub accounts on a single machine can be tricky, especially when you want to keep work and personal projects separate. This guide shows you how to set up SSH keys for different accounts and automatically switch Git identities based on which repository you’re working in.
The Challenge: Managing Multiple GitHub Accounts
If you’re a developer juggling work and personal projects, or managing multiple client accounts, you’ve likely encountered these frustrating issues when working with multiple GitHub accounts on the same computer:
- SSH key conflicts - GitHub only accepts one SSH key per account
- Wrong identity commits - Accidentally committing with your work email to personal repos
- Manual configuration overhead - Having to configure
user.nameanduser.emailfor every repository - Security risks - Accidentally pushing personal code to work repositories (or vice versa)
- Authentication failures - Git using the wrong SSH key for different accounts
The traditional solution involves manually switching between accounts or configuring each repository individually—tedious and error-prone. But there’s a better way.
The Solution
We’ll configure:
- Separate SSH keys for each GitHub account
- SSH config to use the right key automatically
- Git conditional includes to switch identity based on directory
- Nano as the default Git editor
1. SSH Key Setup
1.1 Check Existing Keys
First, check if you already have SSH keys:
ls -al ~/.ssh
Look for files like id_ed25519 or id_rsa. If you have existing keys, you can use them or create new ones.
1.2 Generate SSH Keys
Create separate SSH keys for each account. We’ll use Ed25519 keys which are more secure and faster than RSA.
Work Account:
ssh-keygen -t ed25519 -C "your_work_email@company.com" -f ~/.ssh/id_ed25519_work
Personal Account:
ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/id_ed25519_personal
Press Enter when asked for a passphrase (or set one for extra security).
1.3 Add SSH Keys to GitHub
Copy each public key:
# Work key
cat ~/.ssh/id_ed25519_work.pub
# Personal key
cat ~/.ssh/id_ed25519_personal.pub
Then for each GitHub account:
- Go to GitHub → Settings → SSH and GPG keys → New SSH key
- Give it a descriptive title (e.g., “Work Laptop” or “Personal MacBook”)
- Paste the public key content
- Click “Add SSH key”
1.4 Configure SSH Config
Create or edit your SSH config file:
nano ~/.ssh/config
Add this configuration:
# Work account (default github.com)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
# Personal account (github.com-personal)
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Important: IdentitiesOnly yes prevents SSH from trying other keys.
1.5 Test SSH Connections
Verify both accounts work:
# Work account
ssh -T git@github.com
# Personal account
ssh -T git@github.com-personal
You should see: Hi [username]! You've successfully authenticated...
1.6 Using the Personal Account
When cloning personal repos, use the custom host:
# Instead of:
git clone git@github.com:username/repo.git
# Use:
git clone git@github.com-personal:username/repo.git
For existing repos, update the remote:
git remote set-url origin git@github.com-personal:username/repo.git
2. Git Editor Setup
Set Nano as your default Git editor for commit messages:
git config --global core.editor "nano"
This makes editing commit messages and interactive rebases much easier.
3. Automatic Git Identity Switching
This is where the magic happens! We’ll use Git’s includeIf directive to automatically switch your username and email based on which directory you’re in.
3.1 Directory Structure
First, organize your repositories:
mkdir -p ~/Git/WORK
mkdir -p ~/Git/PERSONAL
Move or clone your repos into the appropriate directories.
3.2 Global Git Config
Edit your main Git config:
nano ~/.gitconfig
Add this configuration:
[user]
name = Your Work Name
email = work@company.com
[filter "lfs"]
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
clean = git-lfs clean -- %f
[core]
editor = nano
# Personal repos
[includeIf "gitdir:~/Git/PERSONAL/"]
path = ~/.gitconfig-personal
# Work repos
[includeIf "gitdir:~/Git/WORK/"]
path = ~/.gitconfig-work
Note: The trailing / in the gitdir path is important!
3.3 Personal Git Config
Create a personal config file:
nano ~/.gitconfig-personal
Add your personal identity:
[user]
name = Your Personal Name
email = personal@example.com
3.4 Work Git Config
Create a work config file:
nano ~/.gitconfig-work
Add your work identity:
[user]
name = Your Work Name
email = work@company.com
3.5 Testing
Verify it works by checking the Git identity in different directories:
# Personal repo
cd ~/Git/PERSONAL/some-repo
git config user.email
# Should output: personal@example.com
# Work repo
cd ~/Git/WORK/some-repo
git config user.email
# Should output: work@company.com
3.6 Removing Local Overrides
If a repository has local user.name or user.email settings, they’ll override the automatic switching. Remove them:
cd /path/to/repo
git config --unset user.email
git config --unset user.name
To bulk remove local overrides in all personal repos:
find ~/Git/PERSONAL -type d -name ".git" -exec sh -c '
cd "${1%/.git}" && git config --unset user.email && git config --unset user.name
' _ {} \;
Complete Workflow Example
Here’s how it all works together:
Cloning a Work Repository
cd ~/Git/WORK
git clone git@github.com:company/project.git
cd project
# Check identity (should be work email)
git config user.email
# Make changes
echo "# Work Project" > README.md
git add .
git commit -m "Initial commit" # Uses work identity automatically
git push origin main
Cloning a Personal Repository
cd ~/Git/PERSONAL
git clone git@github.com-personal:username/personal-project.git
cd personal-project
# Check identity (should be personal email)
git config user.email
# Make changes
echo "# Personal Project" > README.md
git add .
git commit -m "Initial commit" # Uses personal identity automatically
git push origin main
Troubleshooting
Wrong SSH Key Being Used
If you get permission denied:
# Check which key is being used
ssh -vT git@github.com
ssh -vT git@github.com-personal
# Make sure IdentitiesOnly is set in ~/.ssh/config
Wrong Git Identity
If commits show the wrong username/email:
# Check for local config overrides
cd /path/to/repo
git config --list --local | grep user
# Remove local overrides
git config --unset user.email
git config --unset user.name
# Verify automatic config works
git config user.email
SSH Key Permissions
If SSH complains about permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_*
chmod 644 ~/.ssh/*.pub
Summary
✅ SSH Keys: Separate Ed25519 keys for each GitHub account
✅ SSH Config: Custom host aliases (github.com-personal) for routing
✅ Git Editor: Nano set as default for commit messages
✅ Git Identity: Automatic switching based on repository location
✅ Clean Setup: No manual configuration per repository
This setup ensures you’ll never accidentally commit to a work repo with your personal account (or vice versa). The automatic identity switching makes it seamless to work across multiple accounts without thinking about it.
Additional Resources
Have questions or suggestions for improving this setup? Feel free to reach out!