DEV Community

Cover image for A Practical Guide to Using Multiple GitHub Accounts on One Machine (Work + Personal) via SSH
JAYANTH CHALUVADI
JAYANTH CHALUVADI Subscriber

Posted on

A Practical Guide to Using Multiple GitHub Accounts on One Machine (Work + Personal) via SSH

Struggling with Permission denied (publickey) when switching GitHub accounts?

This guide shows you how to configure multiple GitHub accounts using SSH keys, host aliases, and automatic Git identity switching.

Set it up once. It works forever.


πŸš€ Quick Setup (TL;DR)

If you already understand SSH basics, here’s the entire flow:

# 1. Create two SSH keys
ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_personal

# 2. Configure SSH aliases
# ~/.ssh/config
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

# 3. Clone using aliases
git clone git@github-work:org/repo.git
git clone git@github-personal:user/repo.git
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ If this makes sense, you’re done
πŸ‘‰ If not, continue for the full breakdown


The Problem

You have:

  • One work GitHub account
  • One personal GitHub account

But SSH only sees:

github.com
Enter fullscreen mode Exit fullscreen mode

So when you push code, it guesses which key to use. Sometimes it guesses wrong.

Result:

Permission denied (publickey)
Enter fullscreen mode Exit fullscreen mode

The Core Insight (Most Tutorials Miss This)

You are configuring two completely separate systems:

System Responsibility
SSH Authentication (which GitHub account you are)
Git config Commit identity (name and email)

They do not sync automatically.

This is why:

  • You can authenticate as work
  • But commit as personal

Mental Model

Git β†’ SSH Alias β†’ SSH Key β†’ GitHub Account
Enter fullscreen mode Exit fullscreen mode
github-work β†’ id_ed25519_work β†’ Work GitHub
github-personal β†’ id_ed25519_personal β†’ Personal GitHub
Enter fullscreen mode Exit fullscreen mode

Step 1. Open Terminal

  • macOS β†’ Terminal / VS Code terminal
  • Windows β†’ Git Bash (recommended)

Step 2. Organize Repositories

mkdir -p ~/projects/work
mkdir -p ~/projects/personal
Enter fullscreen mode Exit fullscreen mode

This enables automatic Git identity switching later.


Step 3. Generate SSH Keys

ssh-keygen -t ed25519 -C "work@company.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode

Each creates:

  • Private key (never share)
  • Public key (upload to GitHub)

Step 4. Configure SSH (Critical Step)

Edit:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

macOS Configuration (with Keychain)

# Global settings (required for persistence)
Host *
    AddKeysToAgent yes
    UseKeychain yes

# Work account
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

# Personal account
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Windows Configuration

Host *
    AddKeysToAgent yes

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Why aliases are required

Without aliases:

git@github.com
Enter fullscreen mode Exit fullscreen mode

SSH cannot determine which key to use.

With aliases:

git@github-work
git@github-personal
Enter fullscreen mode Exit fullscreen mode

SSH uses the correct key deterministically.


Step 5. Add Keys to SSH Agent

macOS

eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode

Windows (Git Bash)

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_work
ssh-add ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode

macOS Keychain (Important)

  • UseKeychain yes enables secure storage
  • --apple-use-keychain persists keys across restarts

Without this:

  • Keys are lost after reboot
  • You’ll be prompted repeatedly

Step 6. Add Public Keys to GitHub

Copy keys

macOS:

pbcopy < ~/.ssh/id_ed25519_work.pub
Enter fullscreen mode Exit fullscreen mode

Windows:

clip < ~/.ssh/id_ed25519_work.pub
Enter fullscreen mode Exit fullscreen mode

Add in GitHub

  • Profile β†’ Settings β†’ SSH and GPG keys β†’ New SSH key
  • Add separately for:

    • Work account
    • Personal account

Step 7. Test Connections

ssh -T git@github-work
ssh -T git@github-personal
Enter fullscreen mode Exit fullscreen mode

Expected:

Hi <username>! You've successfully authenticated...
Enter fullscreen mode Exit fullscreen mode

Step 8. Automatic Git Identity Switching

Edit:

nano ~/.gitconfig
Enter fullscreen mode Exit fullscreen mode
[user]
    name = Your Work Name
    email = work@company.com

[includeIf "gitdir:~/projects/personal/"]
    path = ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

Create:

nano ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode
[user]
    name = Your Personal Name
    email = personal@example.com
Enter fullscreen mode Exit fullscreen mode

How it works

if repo ∈ ~/projects/personal/
    β†’ use personal identity
else
    β†’ use work identity
Enter fullscreen mode Exit fullscreen mode

Step 9. Clone Repositories Correctly

# Work
git clone git@github-work:company/project.git

# Personal
git clone git@github-personal:username/project.git
Enter fullscreen mode Exit fullscreen mode

Fix existing repositories

git remote set-url origin git@github-work:company/repo.git
Enter fullscreen mode Exit fullscreen mode

Step 10. Verify Setup

git config user.name
git config user.email
Enter fullscreen mode Exit fullscreen mode

Common Issues and Fixes

Permission denied (publickey)

Cause:

github.com used instead of alias
Enter fullscreen mode Exit fullscreen mode

Fix:

git remote set-url origin git@github-work:org/repo.git
Enter fullscreen mode Exit fullscreen mode

Wrong commit identity

git config user.email
Enter fullscreen mode Exit fullscreen mode

Check:

  • Folder structure
  • includeIf configuration

macOS Keychain not working

Ensure:

Host *
    AddKeysToAgent yes
    UseKeychain yes
Enter fullscreen mode Exit fullscreen mode

Re-run:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Windows keys not persisting

PowerShell (Admin):

Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode

Then:

ssh-add ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Quick Reference

Item Work Personal
SSH alias github-work github-personal
Key id_ed25519_work id_ed25519_personal
Folder ~/projects/work ~/projects/personal
Clone git@github-work git@github-personal

Final Thoughts

This setup:

  • Eliminates SSH conflicts
  • Automates identity switching
  • Keeps work and personal environments cleanly separated

One-time setup. Long-term reliability.


If This Helped

  • Save this for future setups
  • Share it with your team

Top comments (0)