Here are step-by-step instructions for configuring and managing multiple GitHub accounts on a single computer, including SSH key setup and Git configuration:

Step 1: Generate SSH Keys for Each GitHub Account

  1. Open your terminal.

  2. Generate a new SSH key for the first GitHub account:

    ssh-keygen -t rsa -b 4096 -C "your_email_first_account@example.com"

    When prompted, save the key with a unique name, e.g., id_rsa_first_account.

  3. Generate a new SSH key for the second GitHub account:

    ssh-keygen -t rsa -b 4096 -C "your_email_second_account@example.com"

    When prompted, save the key with a unique name, e.g., id_rsa_second_account.

Step 2: Add SSH Keys to the SSH Agent

  1. Start the SSH agent in the background:

    eval "$(ssh-agent -s)"
  2. Add the first SSH key to the SSH agent:

    ssh-add ~/.ssh/id_rsa_first_account
  3. Add the second SSH key to the SSH agent:

    ssh-add ~/.ssh/id_rsa_second_account

Step 3: Add SSH Keys to GitHub Accounts

  1. Copy the SSH key for the first account to your clipboard:

    pbcopy < ~/.ssh/id_rsa_first_account.pub

    If pbcopy is not available, you can use:

    cat ~/.ssh/id_rsa_first_account.pub
  2. Log in to your first GitHub account and navigate to Settings > SSH and GPG keys > New SSH key. Paste the key and save.

  3. Repeat the process for the second account:

    pbcopy < ~/.ssh/id_rsa_second_account.pub

    If pbcopy is not available, you can use:

    cat ~/.ssh/id_rsa_second_account.pub
  4. Log in to your second GitHub account and navigate to Settings > SSH and GPG keys > New SSH key. Paste the key and save.

Step 4: Configure SSH to Manage Multiple Accounts

  1. Edit the SSH config file:

    nano ~/.ssh/config
  2. Add the following configurations:

    # First GitHub account
    Host github-first
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_first_account
    
    # Second GitHub account
    Host github-second
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_second_account

Step 5: Configure Git for Multiple Accounts

  1. Set global Git configuration (if not already set):

    git config --global user.name "Your Name"
    git config --global user.email "your_email@example.com"
  2. For repositories associated with the first account, set the local Git configuration:

    cd path/to/your/repository
    git config user.name "Your Name for First Account"
    git config user.email "your_email_first_account@example.com"
  3. For repositories associated with the second account, set the local Git configuration:

    cd path/to/your/other/repository
    git config user.name "Your Name for Second Account"
    git config user.email "your_email_second_account@example.com"

Step 6: Clone Repositories Using the Correct SSH Host

  1. For the first GitHub account, use:

    git clone git@github-first:username/repository.git
  2. For the second GitHub account, use:

    git clone git@github-second:username/repository.git

Step 7: Push and Pull Using the Correct SSH Host

Git will automatically use the correct SSH key based on the configuration in the ~/.ssh/config file.

That’s it! You have now configured and can manage multiple GitHub accounts on a single computer.