Managing Multiple GitHub Accounts: A Comprehensive Guide

Navneet Karnani - Oct 13 - - Dev Community

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 todays digital age, many developers find themselves juggling multiple GitHub accountsone 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Using Gits includeIf Directive

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

  1. Open your global Git configuration file :

  2. Add the includeIf directive:

  3. Create the additional configuration files :

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!

Navneet Karnani is a Full Stack Ployglot veteran, and explores technology to build great products, always striving to extract more productivity from the tools he uses. You can follow him at:

]]>

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player