Skip to content
On this page

    How to Set Up Multiple SSH Keys for GitHub on MacBook

    2 min read

    Today I thought to document how to manage multiple GitHub accounts with different SSH keys on my MacBook. I have two accounts - accountA and accountB - and needed a way to switch between them seamlessly without conflicts.

    Note: I’ve been using this setup for 10+ years; but everytime I go lost on the setup

    What I Assumed:

    • You’re using a macOS (MacBook) environment
    • You have Homebrew installed (for ssh-copy-id)
    • You’re comfortable with the terminal
    • You have two separate GitHub accounts (accountA and accountB)
    • You want to use SSH authentication for both accounts
    • You’re using bash or zsh shell (not fish, though I’ll include fish-specific notes)

    The Solution: Step-by-Step Guide

    Step 1: Generate Separate SSH Keys

    First, generate unique SSH keys for each account:

    # For accountA
    ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_accountA
    
    # For accountB  
    ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_accountB

    When prompted, you can set passphrases or leave them empty for convenience.

    Step 2: Start SSH Agent

    eval "$(ssh-agent -s)"
    
    ## For fish shell
    ssh-agent -c

    Step 3: Add Keys to SSH Agent

    ssh-add -K ~/.ssh/id_ed25519_accountA
    ssh-add -K ~/.ssh/id_ed25519_accountB

    Step 4: Configure SSH Config File

    Create or edit ~/.ssh/config:

    vim ~/.ssh/config

    Add this configuration:

    # accountA configuration
    Host github.com-accountA
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_accountA
      IdentitiesOnly yes
    
    # accountB configuration  
    Host github.com-accountB
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_accountB
      IdentitiesOnly yes

    Step 5: Add SSH Keys to GitHub

    Copy each public key and add to respective GitHub accounts:

    # Copy accountA key
    pbcopy < ~/.ssh/id_ed25519_accountA.pub
    # Go to GitHub accountA → Settings → SSH and GPG keys → Add new
    
    # Copy accountB key  
    pbcopy < ~/.ssh/id_ed25519_accountB.pub
    # Go to GitHub accountB → Settings → SSH and GPG keys → Add new

    Step 6: Clone Repositories with Correct URLs

    # For accountA repositories
    git clone [email protected]:accountA/your-repo.git
    
    # For accountB repositories
    git clone [email protected]:accountB/your-repo.git

    Step 7: Configure Git per Repository

    # In accountA's repository
    git config user.name "Account A"
    git config user.email "[email protected]"
    
    # In accountB's repository
    git config user.name "Account B" 
    git config user.email "[email protected]"

    Testing Your Setup

    # Test accountA connection
    ssh -T [email protected]
    
    # Test accountB connection
    ssh -T [email protected]

    What I Learned

    • SSH config allows you to specify different identities for different hosts
    • IdentitiesOnly yes prevents SSH from trying other keys
    • Each repository needs its own Git user configuration
    • The naming convention (github.com-accountA) makes it clear which account you’re using

    This setup has been working perfectly for me, allowing seamless switching between my two GitHub accounts without any conflicts!

    Additional Tips

    • You can add more accounts by following the same pattern
    • Consider using ~/.ssh/config aliases for commonly used hosts
    • Keep your SSH keys secure and consider using passphrases for added security