Managing Multiple GitHub Accounts: A Comprehensive Guide
- Git
- GitHub
- Devops
- Programming Tips
- Productivity
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:
-
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.
-
Organization: It helps in organizing your repositories and contributions. You can easily distinguish between work-related and personal projects.
-
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_workAdd 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_workConfiguring SSH Config File
Edit your
~/.ssh/configfile to include both keys and block direct access togithub.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_workCloning 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.gitConfiguring 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
includeIfDirectiveYou can set up Git to use different
user.nameanduser.emailconfigurations based on the repository path using Git’sincludeIfdirective. Here’s how:-
Open your global Git configuration file:
vi ~/.gitconfig -
Add the
includeIfdirective:[includeIf "gitdir:~/path/to/personal/repos/"] path = ~/.gitconfig-personal [includeIf "gitdir:~/path/to/work/repos/"] path = ~/.gitconfig-work -
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!
-
-
Originally published at https://blog.mandraketech.in/managing-multiple-git-accounts
-
About the Author
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:
Originally published on Hashnode