All articles
git

Manage Multiple GitHub Accounts using SSH keys

Share this article

Share on LinkedIn Share on X (formerly Twitter)

If you use separate GitHub accounts for work and personal projects, SSH keys help keep them securely separated. This guide works for other Git providers like GitLab and BitBucket too.

Why This Matters

  • Avoid accidental commits to wrong accounts
  • Maintain separate authentication contexts
  • Simplify access to multiple repositories

Step 1: Generate SSH Keys

Run these commands in your terminal:

# Create SSH directory if missing
mkdir -p ~/.ssh && cd ~/.ssh
 
# Generate personal key (replace with your email)
ssh-keygen -t rsa -b 4096 -C "personal@email.com"
# When prompted, name it: id_rsa_personal
 
# Generate work key
ssh-keygen -t rsa -b 4096 -C "work@email.com"
# Name it: id_rsa_work

You'll now have four files:

  • id_rsa_personal (private key)
  • id_rsa_personal.pub (public key)
  • id_rsa_work (private key)
  • id_rsa_work.pub (public key)

🔐 Security Note: Never share private keys (files without .pub)

Step 2: Add Keys to GitHub

  1. Copy each public key to clipboard:

    # Mac/Linux:
    cat ~/.ssh/id_rsa_personal.pub | pbcopy
     
    # Windows:
    clip < ~/.ssh/id_rsa_personal.pub
  2. Paste into GitHub:

    • Go to GitHub SSH Settings
    • Click "New SSH key" → Paste → Save
    • If prompted, select "Authentication Key" as Key type
    • Repeat for work account

Step 3: Create SSH Config File

Create ~/.ssh/config using:

touch ~/.ssh/config  # Creates file
code ~/.ssh/config   # Opens in VSCode (or use any editor)

Add this configuration:

# Personal account
Host personal  # Alias for personal repos
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
 
# Work account
Host work      # Alias for work repos
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

Step 4: Test Your Setup

Verify both accounts work:

ssh -T personal
# Should show: Hi [personal-username]! You've successfully authenticated...
 
ssh -T work
# Should show: Hi [work-username]! You've successfully authenticated...

Using Your Accounts

You need to take care of the remotes of your local repositories, when you are using this multiple SSH keys setup.

Your need to replace github.com in the remote URL with your SSH alias like personal or work

For new repositories

Clone using your aliases:

# Instead of:
git clone git@github.com:user/repo.git
 
# Use:
git clone git@personal:user/repo.git   # Personal account
git clone git@work:user/repo.git       # Work account

For existing repositories

Update remote URL for repositories which are cloned before this setup:

# Personal account
git remote set-url origin git@personal:user/repo.git

⚠️ Important Reminders

  1. Local Git Config:
    Set per-repo Git identity to prevent account mixups:

    # Navigate to repository
    cd ~/projects/personal-project
     
    # Set identity for THIS repository
    git config user.name "Your Personal Name"
    git config user.email "personal@email.com"

    Verify settings with:

    git config user.name
    git config user.email

    Repeat this for every repository! Your commits will be tagged correctly.

  2. Troubleshooting:

    • Use ssh-add ~/.ssh/id_rsa_personal if keys aren't detected
    • Restart terminal after config changes

Pro Tips

This setup lets you keep work and personal contributions separate without constant re-authentication! 🎉


Comments