How to Easily Control Multiple GitHub Accounts with SSH

Managing multiple GitHub accounts on one device can be streamlined by using SSH keys. This guide provides step-by-step instructions to set up and switch between multiple GitHub accounts using SSH on any device.

Step 1: Generate SSH Keys for Each GitHub Account

First, you need to create unique SSH keys for each of your GitHub accounts.

# With Office Email
ssh-keygen -t rsa -b 4096 -C "ashim@programmerlab.com" -f ashim.programmerlab

# With Personal Email
ssh-keygen -t rsa -b 4096 -C "ashimrudrapaul.me@gmail.com" -f ashimrudrapaul.me

Step 2: Create a config file on.sshfile

cd .ssh
touch config
sudo nano config

Add this

# Personal account - default config
Host github.com-ashimrudrapaul.me
        HostName github.com
        User git
        IdentityFile ~/.ssh/ashimrudrapaul.me
# Work account - Programmer Lab
Host github.com-ashim.programmerlab
        HostName github.com
        User git
        IdentityFile ~/.ssh/ashim.programmerlab

Step 3: Create a common~/.gitconfig file at home directory

sudo nano ~/.gitconfig

Add this to~/.gitconfig

# Default account-Personal
[user]
        name = ashimrudrapaul.me
        email = ashimrudrapaul.me@gmail.com
# Professional account-Office
[includeIf "gitdir:~/<office_work_space_path>/"]
        path = ~/<office_work_space_path>/.gitconfig-ashim.programmerlab

Step 4: Create organization-specific.gitconfigto organization workspace folder

# Professional account-organization
[user]
    name = ashim.programmerlab
    email = ashim@programmerlab.com

Step 5: Add the SSH Key to GitHub :

  1. Log in to your GitHub account.

  2. In the upper-right corner of any page, click your profile photo, then click Settings.

  3. In the user settings sidebar, click SSH and GPG keys.

  4. Click New SSH key.

  5. In the "Title" field, add a descriptive label for the new key. For example, "Personal MacBook" or "Work Laptop".

  6. Paste your key into the "Key" field.

  7. Click Add SSH key.

Step 6 : Remove SSH keys and entities from the SSH agent

ssh-add -D

Step 7: Add keys and entities

ssh-add ashimrudrapaul.me
ssh-add ashim.programmerlab

# Check if added or not
ssh-add -l

Step 8: Test with Connection using ssh

ssh -T github.com-ashimrudrapaul.me
ssh -T github.com-ashim.programmerlab

Also read this aricle on my personalgist

Step 9: Cloning any project with an organization account(required)

# need to add github-hostname
git clone git@<HostName>:ashim.programmerlab/<office_project_repo>.git

Example: git clone git@github.com-ashim.programmerlab:programmerlab/<office_project_repo>.git
2
Subscribe to my newsletter

Read articles from Ashim Rudra Paul directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashim Rudra Paul
Ashim Rudra Paul

I am a Software Engineer at SJ Innovation with over 2 years of experience specializing in the MERN stack. My expertise spans TypeScript, Next.js, React.js, React Native, Express.js, Firebase, Supabase, MongoDB, PostgreSQL, and MySQL. I hold a degree in Computer Science and Engineering from Sylhet Polytechnic Institute and have earned certifications in MERN stack web development and JavaScript, C, C++, Python programming. Previously a Team Lead at elPixala, I excel in collaborating with product and design teams to create seamless user experiences. I prioritize rigorous testing and debugging to ensure high-quality performance. What sets me apart is my passion for coding and problem-solving. I thrive on crafting innovative solutions to complex challenges. Recently venturing into competitive programming on platforms like HackerRank and Codeforces has further sharpened my skills. Choose me for my proven track record of delivering dynamic web applications that combine functionality with visual appeal. My commitment to quality ensures top-notch results every time.