Setting up multiple GitHub accounts, the nicer way (2024)

We previously wrote about setting up multiple GitLab accounts, which works by relying mostly on SSH config files and using custom hosts as remotes for git repositories. You can read it by following the link below:

The previous setup works, but I find it inconvenient having to use custom hosts when cloning repositories or for git remotes all the time, like:

$ git clone git@gitlab.com-work:work/repository.git

I recently (almost) had to deal with the same scenario, on GitHub this time (hence the title), so I decided to check if I can achieve a better setup. Turns out that there’s a better way to do it — one that does not rely on custom hosts or modifying any SSH config file.

In this article, we are going to assume that we have two GitHub accounts, [email protected] and [email protected]. When working on work-related projects we want to commit and push our changes using our work account, while using our personal account for everything else.

We’re also going to assume that we’ve already defined global configs telling git to use our personal account. See this guide on how to do that.

GitHub does not allow us to use the same SSH key in multiple accounts, so we’ll have to create separate keys for each account. We can create SSH keys and add them to our SSH agent by following this guide from the GitHub Documentation.

Once we’re done, we will have two sets of SSH keys, e.g.:

  • ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub (let’s use this for our personal account)
  • ~/.ssh/work_key and ~/.ssh/work_key.pub

Now add the SSH keys to their corresponding GitHub accounts. This guide shows us how to do that.

We then modify our ~/.gitconfig file to use our work account when we are inside a work-related repository. We are going to use “Conditional Includes” for that, which dynamically adds/removes configuration based on certain conditions. Let’s add the following snippet to our ~/.gitconfig file:

[includeIf "gitdir:/path/to/work/repository/"]
[user]
email = "[email protected]"

This makes git override our global configs and use [email protected] when committing changes inside the work repository. When in any other repository, our personal account will be used.

Do this for all work repositories

The way I personally structure my repositories on my file system is I place all work-related repositories inside the ~/work directory. This makes it easier to enable the configs above for all work-related repositories by just using the work directory in the includeIf directive:

[includeIf "gitdir:~/work/"]

When we push our commits now, they will be pushed to our personal GitHub account, even though we’ve already committed them using our work email. That’s because under the hood, git uses SSH, and SSH uses the id_rsa key by default. So when GitHub receives the push, it will try to determine which user pushed the commits based on the SSH key used, which will turn out to be our personal account.

We can resolve this by using the work_key SSH key when pushing to our work repositories. In SSH, this can be done through the -i flag, like so:

$ ssh -i ~/.ssh/work_key ...

We can configure git to use a custom SSH command like above by setting core.sshCommand. And since we only want to set it when we’re inside any work repository, we also conditionally include it inside the includeIf directive:

[includeIf "gitdir:/path/to/work/repository/"]
[user]
email = "[email protected]"
[core]
sshCommand = "ssh -i ~/.ssh/work_key"

These configs now make git commit using our work email, as well as make GitHub associate commits with our work account when we push them 🎉.

We’ve managed to configure git to use different configs when committing and associating these commits to different GitHub accounts when pushing, based on what repository we are performing those changes — and we used conditional includes in git’s config file to achieve that.

I like this way better than the previous setup since it’s more convenient to use (no more custom hosts) and all the config changes only affect git (no more changing of SSH config files).

If you deal with these multiple GitHub accounts in a different way, please feel free to share with us how you do it as well!

Setting up multiple GitHub accounts, the nicer way (2024)
Top Articles
How to Start Investing in the Stock Market - Zero To Freedom
Grayscale challenges SEC on Blackrock filing to ensure fair approval of Bitcoin ETPs
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6569

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.