Learn how to maintain multiple Git accounts on a single machine efficiently. This guide covers generating SSH keys, configuring SSH and Git settings, and automating the process with scripts. Perfect for developers managing personal and professional repositories
Reviewed and edited by Spandan
As developers, we often find ourselves working on multiple projects, each requiring different Git accounts.
For compliance reasons access to customer repositories may only be provided to those signed in with an organization's email ID. This can conflict with the existing GitHub configuration if the system was initially set up with only the use of only one account in mind.
Managing these accounts can be cumbersome, especially when you need to switch between them frequently. In this blog post, we will explore how to maintain multiple Git accounts on a single machine efficiently.
Steps to Follow
Configure SSH to use the right SSH key:
Ensure that the correct SSH key is used depending on the repository organization. This will help access each repository with the appropriate account.
Configure GitHub config to use the right username and email ID:
Set up the GitHub configuration to use the correct username and email address for each account. This ensures that the Git log history is accurate and reflects the correct account for each commit.
Sample Setup
We will be using the following sample scenario to demo this setup:
Self: Managing personal repositories and open source contributions using a personal email ID.
One2N: Managing One2N’s repositories using One2N’s email ID.
Configuring SSH Keys
We followed this guide to generate and add SSH keys to the personal and One2N account. Let’s assume the following keys were created:
Self -
~/.ssh/id_rsa_self
One2N -
~/.ssh/id_rsa_one2n
Update the SSH config to associate the right key with the right host when managing repos:
Use the right SSH host name when working with repositories. For example, if one wants to clone a repo in the One2N org, use the following command:
If one wants to use the
id_rsa_self
key, change the hostname accordingly:
Setup GitHub Config
By just setting up SSH keys, one can manage their GitHub repos but one might also require specific user.name
and user.email
settings per each org. This is where having custom Git config helps.
Having a top-level .gitconfig
that refers to each category of .gitconfig
depending on the context is useful here.
Let us look at the content of each file:
Creating repositories within self
and the organizations repositories within one2n
is useful to isolate the repos and have separate configurations.
The above setup can help use common configuration options in ~/.gitconfig
and specific ones in ~/.gitconfig.self
and ~/.gitconfig.one2n
.
Extending this Further
One can update ~/.ssh/config
and ~/.gitconfig
with as many new organization accounts as required.
Wrapping up
Managing multiple GitHub accounts on a single machine is entirely feasible with the right setup. By configuring SSH keys and Git settings correctly, anyone can seamlessly switch between accounts without the risk of cross-contamination between personal and professional repositories. Following the steps outlined in this guide will help maintain a smooth workflow and keep projects organized.