How To Setup 2 Github Accounts On Same Machine

Abhishek Jaiswal
3 min readDec 9, 2023

--

Photo by Roman Synkevych on Unsplash

As developers, we frequently have to work with multiple GitHub accounts on a regular basis.

These accounts could be linked to a professional email and a personal one or even another email that we use occasionally, and we might have to switch between them often.

You only need four simple steps to get them set up and you are done to manage 2 different github account from 2 different folders, we will be using naming as “personal” and “office”.

Here we go 👇

Generating SSH Keys

To generate a new SSH key for each GitHub account, open your terminal and run the following command, replacing “your_email@example.com” with the email address associated with the GitHub account:

1. ssh-keygen -t ed25519 -C "personal@example.com"

When prompted for a file in which to save the key, choose a unique name and location, such as ~/.ssh/id_ed25519_personal.

2. ssh-keygen -t ed25519 -C "office@example.com"

When prompted for a file in which to save the key, choose a unique name and location, such as ~/.ssh/id_ed25519_office.

Add the SSH key to the SSH agent

To add the newly generated SSH key to the SSH agent, run these commands in your terminal:

1. eval "$(ssh-agent -s)"
2. ssh-add ~/.ssh/id_ed25519_personal
3. ssh-add ~/.ssh/id_ed25519_office

Create a global Git configuration

Create the global .gitconfig file in your home directory if it doesn't already exist. Then add all the profile directories as an entry like in the example below.

[includeIf "gitdir:~/personal/"]
path = ~/.ssh/.gitconfig-personal
[includeIf "gitdir:~/office/"]
path = ~/.ssh/.gitconfig-work

Replace ~/personal/ and ~/office/ with the file directory folder you want to give access to work upon.

Create individual Git configurations for profiles

If you haven’t noticed by now, we just mentioned the .gitconfig-personal and .gitconfig-work files in the global .gitconfig file, but we didn't create them yet. These individual files can contain all the customization that you need, from user name and email to commit hooks.

Create file under ~/.ssh/.gitconfig-personal and paste the below item

[user]
name = <replace_personal_user>
email = <replace_personal_email>

Create file under ~/.ssh/.gitconfig-office and paste the below item

[user]
name = <replace_office_user>
email = <replace_office_email>

Configuring GitHub Accounts

Create a new file called config in the ~/.ssh directory. In this file, add the following lines:

Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal

Host github.com-office
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_office

Test the configuration

To test your configuration, run the following command for each account:

1. ssh -T git@github.com-personal
2. ssh -T git@github.com-office

The setup part is done…..

Photo by Yancy Min on Unsplash

In-order to use the 2 accounts, you only need to place your respective folder created i.e. ~/personal/ and ~/office/ using

TO CLONE USING PERSONAL ACCOUNT to ~/personal/ : git@github.com-personal:personal/xyz.git

TO CLONE USING OFFICE ACCOUNT to ~/office/ : git@github.com-office:office/xyz.git

Now proceed with the normal github commands to pull, commit, push..

1. git add .
2. git commit -m "add message"
3. git push origin main

To check your respective username and userEmail, you can go to your respective folder and type

1. git config user.name
2. git config user.email

Hope you did like it, follow for more life saving articles…

--

--