Skip to main content

Managing Multiple GitHub Accounts on Ubuntu

· 4 min read
Hassan Ali
Front End Engineer

In this article, we will learn how to manage multiple Git accounts on a single system using SSH keys. This is useful when you have multiple Git accounts (e.g., personal and work) and want to use them on the same machine without conflicts.

Introduction

When you have multiple Git accounts, you need to manage them properly to avoid conflicts. The best way to manage multiple Git accounts is to use SSH keys. SSH keys allow you to authenticate with Git servers without entering your username and password every time.

Prerequisites

Before you start, make sure you have the following prerequisites:

  • A system running Ubuntu
  • Git installed on your system
  • A GitHub account

Step 1: Generate SSH Keys

  • Before generating an SSH key, we can check to see if we have any existing SSH keys:
ls -al ~/.ssh

This will list out all existing public and private key pairs, if any. If ~/.ssh/id_rsa is available, we can reuse it, or else we can first generate a key to the default ~/.ssh/id_rsa by running:

ssh-keygen -t rsa
  • If you want to generate a key with a different name, you can specify the name of the key file by running:
ssh-keygen -t rsa -f ~/.ssh/id_rsa_personal
  • When asked for the location to save the keys, accept the default location by pressing enter.
  • A private key and public key ~/.ssh/id_rsa.pub will be created at the default ssh location ~/.ssh/

For the work accounts, we will create different SSH keys. The below code will generate the SSH keys, and saves the public key with the tag “email@work_mail.com” to ~/.ssh/id_rsa_work_user1.pub

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"

We have two different keys created:

~/.ssh/id_rsa
~/.ssh/id_rsa_work_user1

Step 2: Add SSH Keys to SSH Agent

  • To add the SSH keys to the SSH agent, run the following command:
ssh-add ~/.ssh/id_rsa
  • If you have multiple keys, you can add them all by running:
ssh-add ~/.ssh/id_rsa_work_user1

Step 3: Add SSH Keys to GitHub

  • To add the SSH keys to GitHub, you need to copy the public key and add it to your GitHub account. You can copy the public key by running:
cat ~/.ssh/id_rsa.pub
  • Copy the output and add it to your GitHub account. Go to your GitHub account, click on your profile picture, and select Settings. In the Settings page, click on SSH and GPG keys and then click on New SSH key. Paste the public key in the Key field and click on Add SSH key.

  • Repeat the same process for the work account by copying the public key ~/.ssh/id_rsa_work_user1.pub and adding it to the work GitHub account.

Step 4: Configure SSH Config File

  • To configure the SSH config file, run the following command:
nano ~/.ssh/config

if open in visual studio code

code ~/.ssh/config
  • Add the following configuration to the SSH config file:
# Personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# Work account
Host github.com-work-account
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work_user1
  • Save the file and exit the editor.

Step 5: Clone Repositories

  • To clone repositories using the personal account, run the following command:
Syntax
git@github.com:<username>/<repo-name>.git
Personal Account
git@github.com:username/repo-name.git
Work Account
git@github.com-work:username/repo-name.git

e.g

git@github.com-work-account:username/repo-name.git

Set Already Cloned Repositories

  • If you have already cloned repositories and setup the SSH key now then change the remote URL by running the following command:

  • First navigate to the repository directory:

git remote -v
Setup Personal Account
git remote set-url origin git@github.com:<github username>/<repo-name>.git
Setup Work Account
git remote set-url origin git@github.com-work-account:<github username>/<repo-name>.git

Setup Local configuration

  • To set the local configuration for the repository, run the following command:
git config user.name "Hassan Ali"
git config user.email "contact@hassanali.pk"

References