Skip to content

Instantly share code, notes, and snippets.

@leifarriens
Created March 11, 2025 18:57
Show Gist options
  • Save leifarriens/a2d86c829e8f9853a9c418a4e942ff43 to your computer and use it in GitHub Desktop.
Save leifarriens/a2d86c829e8f9853a9c418a4e942ff43 to your computer and use it in GitHub Desktop.
Multiple GitHub accounts on a single computer

Step 1: Generate SSH keys for each GitHub account

  1. Open a terminal on your computer.

  2. Generate a new SSH key for your first GitHub account. Replace [email protected] with the email associated with your GitHub account.

    ssh-keygen -t ed25519 -C "[email protected]"

    When prompted, save the key with a descriptive name, such as id_ed25519_first_account.

  3. Repeat the process for your second GitHub account (and any additional accounts). Use a different descriptive name for each key, such as id_ed25519_second_account.

Step 2: Add the SSH keys to your SSH agent

  1. Start the SSH agent in the background:

    eval "$(ssh-agent -s)"
  2. Add the SSH keys to the agent. Replace path_to_key with the path to the SSH key files you generated earlier:

    ssh-add ~/.ssh/id_ed25519_first_account
    ssh-add ~/.ssh/id_ed25519_second_account

Step 3: Add SSH keys to your GitHub accounts

  1. Copy the SSH key to your clipboard. Replace path_to_key with the path to the SSH key file:

    pbcopy < ~/.ssh/id_ed25519_first_account.pub

    Or, if pbcopy is not available, you can manually copy the key:

    cat ~/.ssh/id_ed25519_first_account.pub
  2. Add the SSH key to your GitHub account:

    • Go to your GitHub account settings.
    • Navigate to SSH and GPG keys.
    • Click New SSH key.
    • Paste the copied key and give it a descriptive title.
    • Repeat for the second (and any additional) GitHub accounts.

Step 4: Configure SSH for multiple GitHub accounts

  1. Edit the SSH configuration file (~/.ssh/config) to define configurations for each GitHub account. If the file does not exist, create it:

    nano ~/.ssh/config
  2. Add configurations for each account:

    # Configuration for first GitHub account
    Host github.com-first
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_first_account
    
    # Configuration for second GitHub account
    Host github.com-second
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_second_account

Step 5: Configure Git for multiple accounts

  1. Set up global Git configuration (used as a fallback):

    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
  2. Set up repository-specific Git configurations. For each repository, configure Git to use the appropriate account:

    cd path/to/your/repository
    git config user.name "Your Name for First Account"
    git config user.email "[email protected]"
    git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_first_account -F /dev/null"

    For repositories associated with the second account, use the corresponding SSH key:

    cd path/to/your/second-repository
    git config user.name "Your Name for Second Account"
    git config user.email "[email protected]"
    git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_second_account -F /dev/null"

Step 6: Clone repositories using the correct SSH configuration

  1. When cloning a repository, use the Host alias defined in your SSH configuration (~/.ssh/config). For example:

    git clone [email protected]:username/repository.git
    git clone [email protected]:username/repository.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment