Friday, April 25, 2025

Published April 25, 2025 by with 0 comment

๐Ÿ™ How to Set Up Multiple GitHub Accounts on the Same Machine



 Managing multiple GitHub accounts (like personal and work) on the same machine can be tricky if not set up properly. This guide walks you through setting up separate SSH keys, configuring Git, and switching seamlessly between accounts.

✅ Prerequisites

Before diving in, make sure you have:

  • Git installed on your system. (git --version)

  • A GitHub account (or two).

  •  Basic knowledge of command line usage.


๐Ÿ—️ Step 1: Generate SSH Keys for Each Account

We'll generate separate SSH keys for your personal and work accounts.

# Personal GitHub account

ssh-keygen -t rsa -b 4096 -C "you@example.com" -f ~/.ssh/id_rsa_personal


# Work GitHub account

ssh-keygen -t rsa -b 4096 -C "you@work.com" -f ~/.ssh/id_rsa_work

This creates two key pairs:

  • ~/.ssh/id_rsa_personal & ~/.ssh/id_rsa_personal.pub

  • ~/.ssh/id_rsa_work & ~/.ssh/id_rsa_work.pub


๐Ÿ”— Step 2: Add SSH Keys to GitHub

Go to GitHub → Settings → SSH and GPG keys → New SSH key
Add:

  • For personal: id_rsa_personal.pub

  • For work: id_rsa_work.pub

Use a meaningful title for each key (e.g., “Personal Laptop”, “Work Laptop”).


๐Ÿงผ Step 3: Adding SSH Keys to the SSH agent

To add the newly generated SSH key to the SSH agent, run these commands in your terminal:

    eval "$(ssh-agent -s)"

    ssh-add ~/.ssh/id_rsa_personal

    ssh-add ~/.ssh/id_rsa_work


Repeat these steps for each GitHub account you want to add, remembering to replace the file paths with the unique names you chose earlier.


๐Ÿ› ️ Step 4: Create or Edit SSH Config

Open (or create) your SSH config file:

    nano ~/.ssh/config

Add the following configuration:

    # Personal GitHub account

    Host github.com-personal

          HostName github.com

          User git

          IdentityFile ~/.ssh/id_rsa_personal


    # Work GitHub account

     Host github.com-work

          HostName github.com

          User git

          IdentityFile ~/.ssh/id_rsa_work


๐Ÿงช Step 5: Test SSH Connections

        ssh -T git@github.com-personal

        ssh -T git@github.com-work

You should see:

       Hi <username>! You've successfully authenticated… for both.


๐Ÿ“‚ Step 6: Clone Repositories with Correct Host

When cloning, make sure you use the correct Host alias:

# Personal repo
git clone git@github.com-personal:your-username/repo-name.git

# Work repo
git clone git@github.com-work:work-username/repo-name.git


๐Ÿ”ง Step 7: Set Git Identity Per Project

You can set identity per project so Git uses the right user info:

    cd ~/path/to/personal-repo
    git config user.name "Your Name"
    git config user.email "you@example.com"

    cd ~/path/to/work-repo
    git config user.name "Your Work Name"
    git config user.email "you@work.com"


๐Ÿง  Conclusion

Managing multiple GitHub accounts on the same machine is totally doable with a little SSH and Git config magic. By separating your keys and using SSH aliases, you can avoid headaches and context-switch seamlessly.

Now you can:

✅ Work with both personal and professional repos
✅ Keep identities clean and separate
✅ Avoid constant re-authentication or mistakes


      edit

0 comments:

Post a Comment