Services

Resources

Company

Github

Aug 5, 2024 | 3 min read

Maintaining Multiple GitHub Accounts on a Single Machine

Maintaining Multiple GitHub Accounts on a Single Machine

Principal SRE @One2N

Github

Aug 5, 2024 | 3 min read

Maintaining Multiple GitHub Accounts on a Single Machine

Principal SRE @One2N

Github

Aug 5, 2024 | 3 min read

Maintaining Multiple GitHub Accounts on a Single Machine

Principal SRE @One2N

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

  1. 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.

  2. 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:

  1. Self: Managing personal repositories and open source contributions using a personal email ID.

  2. One2N: Managing One2N’s repositories using One2N’s email ID.

Configuring SSH Keys

  1. We followed this guide to generate and add SSH keys to the personal and One2N account. Let’s assume the following keys were created:

    1. Self - ~/.ssh/id_rsa_self

    2. One2N - ~/.ssh/id_rsa_one2n

  2. Update the SSH config to associate the right key with the right host when managing repos:

    # Self GitHub account
    Host github.com
        User git
        Hostname github.com
        IdentityFile ~/.ssh/id_rsa_self
    
    # One2N GitHub account
    Host github.com-one2n
        User git
    
    
  3. 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:

    git clone git
  4. If one wants to use the id_rsa_self key, change the hostname accordingly:

    git clone git

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:

cat ~/.gitconfig

[url "ssh://git@github.com/"]
  insteadOf = <https://github.com/>
[includeIf "gitdir:~/github/self/"]
  path = .gitconfig.self
[includeIf "gitdir:~/github/one2n/"

Creating repositories within self and the organizations repositories within one2n is useful to isolate the repos and have separate configurations.

cat

cat

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.

Share

Jump to Section

Also Checkout

Also Checkout

Also Checkout

Subscribe for more such content

Stay updated with the latest insights and best practices in software engineering and site reliability engineering by subscribing to our content.

Subscribe for more such content

Stay updated with the latest insights and best practices in software engineering and site reliability engineering by subscribing to our content.

Subscribe for more such content

Stay updated with the latest insights and best practices in software engineering and site reliability engineering by subscribing to our content.