Skip to content

Use multiple Github accounts from your machine

Posted on:May 29, 2024 at 12:20 AM

tp.web.random_picture

A guide on how to operate multiple Github accounts from your machine.

Table of contents

Open Table of contents

Overview

As a freelancer I have to work with multiple Github accounts that belong to different clients of mine. Typing password at each git push or pull is a wastage of time. It is also painful to remember credentials for multiple accounts.

Solution

You can access and write data in repositories on GitHub.com using SSH (Secure Shell Protocol). I know that’s something common we all know. However, we can define the host aliases in our ssh config file for each account.

Steps

  1. You must have generated ssh key pairs for accounts and then have added them to Github accounts.
  2. Edit or create ssh config file at ~/.ssh/config.
# Primary (Default) github account: ManadayM
Host github.com
	Hostname github.com
	IdentityFile ~/.ssh/manadaym_private_key
	IdentitiesOnly yes

# Secondary github account: clientname
Host github-clientname
	Hostname github.com
	IdentityFile ~/.ssh/client_private_key
	IdentitiesOnly yes
  1. Add your SSH keys to the ssh-agent
$ ssh-add ~/.ssh/manadaym_private_key
$ ssh-add ~/.ssh/client_private_key
  1. Test your connection
# Fetches the public SSH keys from github.com and appends them to the local known_hosts file.
$ ssh-keyscan github.com >> ~/.ssh/known_hosts

# Tests the SSH connections.
$ ssh -T git@github.com
$ ssh -T git@github-clientname

By now you should see the following messages.

Hi ManadayM! You've successfully authenticated, but GitHub does not provide shell access.
Hi ClientName! You've successfully authenticated, but GitHub does not provide shell access.
  1. You’re all set!
git@github-clientname:clientname/project.git => user is clientname
git@github.com:ManadayM/project.git.     => user is ManadayM
$ git clone git@github-clientname:clientname/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "manaday@client.com"
$ git config user.name  "Manaday Mavani"
$ cd /path/to/project2
$ git remote set-url origin git@github-clientname:clientname/project2.git
$ git config user.email "manaday@client.com"
$ git config user.name  "Manaday Mavani"

That’s all folks!

PS - While I’m using this idea since long, I decided to document the steps for my future reference. A quick Googling on this topic brought me to the referenced gist by oanhnn. I have borrowed/copied many parts of his gist for this note. Hence, all credit goes to him for the writeup. 🙏🏼😅

References