SGDG

Hi! My name is SyL 👋
This is where I invite you to explore my journey as a solo entrepreneur. Join me on this adventure 🚀, where I share valuable insights and experiences to empower your entrepreneurial path. 🌟

Managing multiple Github accounts on one PC

I'll show you how to manage multiple Github accounts on one computer.

Written by
SyL
Published on
09 Oct 2023

Introduction

When I started a new Github account for SGDS, I run into a problem where I could not push my files into Github. It was always using my personal Github account keypairs instead of the one I created for SGDS. So I hope this article will help you on this.

Create new SSH key

Fire up a terminal and head to your .ssh folder, it should be under your home directory. If you are a Windows user, use Windows Powershell terminal to run this.

cd ~/.ssh

Generate a new SSH key for our each of our Github accounts by running the command below.
You will be prompted for a passphrase, you can enter one or skip it.

ssh-keygen -t rsa -C "github-email@example.com" -f "github-username"

-t specifies the type of key to be created.
-C is the comment for the key and we will use our Github email
-f specifies the name of the file and we will use our Github username

Add SSH keys to Github

We need to add the new keys to our Github accounts. To view our keys, we can run this command below. Make sure to replace the <github-username> with the same one that you used for -f option above.

cat <github-username>.pub 

Login to your Github.com account to add new SSH Key under SSH and GPG keys or click here and click on New SSH key.

Add SSH Keys to SSH Agent

To start using our SSH keys, we have to add them to ssh-agent. You can do so by entering this command below for each key. Make sure to replace the <github-username> with the same one that you used for -f option above.

ssh-add <github-username>

Create a Config file

We are almost done, we just have to create a config file under ~/.ssh/config to create a host entry for each Github account. You can run this command if the config file does not exists.

touch ~/.ssh/config

Use this command instead if you are using Windows Powershell

New-Item ~/.ssh/config

Once created, open the ~/.ssh/config file with with any text editor of your choice such as Notepad or VS Code and enter the following for each Github account. Make sure to replace the <github-username> with the same one that you used.

#Github Username 1
Host github.com-<github-username>
  HostName github.com
  User git
  IdentityFile ~/.ssh/<github-username>

#Github Username 2
Host github.com-<github-username-2>
  HostName github.com
  User git
  IdentityFile ~/.ssh/<github-username-2>

..add more if required

Work with the right Github account

We can now choose to work on our project with the correct github account hosts. Look at the commands below and note that instead of using git@github.com, we're using the custom host we created in the config file: git@github.com-<github-username>.

Clone your repo with:

 git clone git@github.com-<github-username>:{repo-github-username}/{repo-name}.git
 [example:] git clone git@github.com-syl-personal:SGDS-SyL/nuxt-starter-blog.git

Add your remote with:

git remote add origin git@github.com-<github-username>:{repo-github-username}/{repo-name}.git
[example:] git remote add origin git@github.com-syl-work:SGDS-SyL/nuxt-starter-blog.git

Edit your remote with:

git remote set-url origin git@github.com-<github-username>:{repo-github-username}/{repo-name}.git
[example:] git remote add origin git@github.com-syl-work:SGDS-SyL/nuxt-starter-blog.git

✨ You have now completed the setup for managing multiple github account on one computer!