Managing Multiple GitHub Accounts: A Comprehensive Guide

Managing multiple git accounts, on the same desktop user is a challenge. This article provides one solution.

A lot of organisations these days use Github for hosting their repositories. With BYOD as a normal practice, everyone wants to be able to differentiate their Git access for work from the one for personal use. Here is a way in which SSH based git access can be streamlined, so that you can easily switch between work and personal work, and never make errors

  • The intention

    In today’s digital age, many developers find themselves juggling multiple GitHub accounts—one for personal projects and another for work. Managing these accounts efficiently can be a bit tricky, but with the right setup, you can seamlessly switch between them. This guide will walk you through the process of configuring your Git and SSH settings to handle multiple GitHub accounts on the same machine.

    Why Separate Accounts?

    Using separate email addresses for personal and work repositories on GitHub is a good practice for several reasons:

    1. Privacy and Security: Keeping your work and personal emails separate helps protect your privacy and maintain security. Your work email might have different security policies and access controls compared to your personal email.

    2. Organization: It helps in organizing your repositories and contributions. You can easily distinguish between work-related and personal projects.

    3. Professionalism: Using your work email for professional repositories ensures that your contributions are recognized by your employer and colleagues.

Setting Up SSH Keys

First, generate separate SSH keys for your personal and work accounts:

    # Personal account
    ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com"
    # Save the key as ~/.ssh/id_rsa_personal

    # Work account
    ssh-keygen -t rsa -b 4096 -C "your_work_email@example.com"
    # Save the key as ~/.ssh/id_rsa_work

Add the generated SSH keys to the SSH agent:

    # Start the SSH agent
    eval "$(ssh-agent -s)"

    # Add personal key
    ssh-add ~/.ssh/id_rsa_personal

    # Add work key
    ssh-add ~/.ssh/id_rsa_work

Configuring SSH Config File

Edit your ~/.ssh/config file to include both keys and block direct access to github.com:

    # Block direct access to github.com, to never make an error
    Host github.com
      HostName github.com
      User git
      IdentityFile /dev/null

    # Personal account
    Host github-personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_personal

    # Work account
    Host github-work
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_work

Cloning Repositories

When cloning repositories, use the appropriate host alias:

    # Personal repository
    git clone git@github-personal:username/repo.git

    # Work repository
    git clone git@github-work:username/repo.git

Configuring Git for Each Repository

Ensure you set the correct email for each repository:

    # For personal repositories
    cd path/to/personal/repo
    git config user.name "Your Name"
    git config user.email "your_personal_email@example.com"

    # For work repositories
    cd path/to/work/repo
    git config user.name "Your Name"
    git config user.email "your_work_email@example.com"

Using Git’s includeIf Directive

You can set up Git to use different user.name and user.email configurations based on the repository path using Git’s includeIf directive. Here’s how:

  1. Open your global Git configuration file:

     vi ~/.gitconfig
    
  2. Add the includeIf directive:

     [includeIf "gitdir:~/path/to/personal/repos/"]
       path = ~/.gitconfig-personal
    
     [includeIf "gitdir:~/path/to/work/repos/"]
       path = ~/.gitconfig-work
    
  3. Create the additional configuration files:

     # ~/.gitconfig-personal
     [user]
       name = Your Personal Name
       email = your_personal_email@example.com
    
     # ~/.gitconfig-work
     [user]
       name = Your Work Name
       email = your_work_email@example.com
    

Restricting Access to Private Repositories

To restrict access to private repositories and ensure that only public repositories are accessible, you might need to use network-level controls or firewall settings. This approach can be complex and might require additional tools or configurations.

Conclusion

By following these steps, you can effectively manage both your personal and work repositories on GitHub without confusion. Whether you choose to use different SSH keys, configure Git settings based on repository paths, or employ network-level restrictions, these practices will help you maintain a clear separation between your personal and professional projects.

If you have any questions or need further assistance, feel free to reach out!

Did you find this article valuable?

Support MandrakeTech Blog by becoming a sponsor. Any amount is appreciated!