Blog
GitHub Setup with SSH for Multiple Accounts
Back to Articles

GitHub Setup with SSH for Multiple Accounts

· 5 min read read#SSH#github#git

SSH (secure shell)

SSH means Secure Shell — it’s a protocol that allows two computers to talk securely.

For GitHub:

  • SSH allows your computer to prove your identity to GitHub
  • Without typing your username or password every time
  • Using keys (a key pair: public + private)

SSH Keys: Think of Them Like a Lock & Key

SSH uses two keys:

Private Key (stored on your computer)

  • Stays in ~/.ssh/id_rsa or similar
  • You never share this
  • Acts like your personal secret key

Public Key (added to GitHub)

  • Looks like ssh-rsa AAAAB3....
  • You upload this to GitHub
  • Safe to share — it’s just a lock

When you “push” your code:

GitHub checks if your private key matches the public key saved on GitHub.

If yes → GitHub says “OK that is really you”

To Manage Multiple GitHub Account in Vs Code. we can configure SSH keys and use a different Git configuration for each account

Step 1: Generate SSH Keys for Both Accounts

  1. Open a terminal in VS Code.

  2. Generate SSH key for your personal account:

    ssh-keygen -t rsa -b 4096 -C "personalAccount@gmail.com"
    • Save the key as ~/.ssh/id_rsa_personal.
  3. Generate SSH key for your office account:

    ssh-keygen -t rsa -b 4096 -C "sOffice@work.com"
    • Save the key as ~/.ssh/id_rsa_office.

Step 2: Add SSH Keys to the SSH Agent

  1. Start the SSH agent:

    eval "$(ssh-agent -s)"
  2. Add your personal key:

    ssh-add ~/.ssh/id_rsa_personal
  3. Add your office key:

    ssh-add ~/.ssh/id_rsa_office

    Step 3: Add SSH Keys to GitHub

  4. Copy the SSH keys to the clipboard:

    • For personal:
      cat ~/.ssh/id_rsa_personal.pub
    • For office:
      cat ~/.ssh/id_rsa_office.pub
  5. Add the SSH keys to your GitHub accounts:

    • Go to GitHub Settings > SSH and GPG keys.
    • Add a new SSH key and paste the content from the respective id_rsa_personal.pub and id_rsa_office.pub.

Step 4: Configure SSH Config File

  1. Edit your SSH config file:

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

    # Personal GitHub account
    Host github.com-personal
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_personal
     
    # Office GitHub account
    Host github.com-office
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_office
     

Step 5: Clone Repositories Using the Correct SSH Config

  1. For personal projects:

    git clone git@github.com-personal:username/repository.git
  2. For office projects:

    git clone git@github.com-office:username/repository.git

Step 6: Configure Git User for Each Repository

  1. Navigate to your project directory.

  2. Set the user for personal projects:

    git config user.name "Your Name"
    git config user.email "personalAccountgmail.com"
     
  3. Set the user for office projects:

    git config user.name "Your Name"
    git config user.email "offiec@work.com"

Exception

  • If you have configured Git globally, you may encounter issues where commits or pushes use the globally set account name, even though that account doesn't have permission to access the repository.
git config --global user.name "Your Name"
git config --global user.email "offiec@work.com"
  • To Fix This we have remove git config globally
git config --global --unset user.name git config --global --unset user.email