Last Updated on September 27, 2024

Are you in between personal and work projects or maybe just managing multiple professional accounts from a single machine? Then, this article is here to help you simplify your Git configuration and management processes.

Managing multiple GitHub accounts on a single machine doesn’t have to be complicated. In this article, we will analyze the need for proper setup along with management of GitHub accounts and how it helps to protect important data which is often at risk of human error and cyber threats. 

Why would I need multiple GitHub accounts? 

If you actually decide to set up multiple GitHub accounts, it can benefit you in a number of different ways, depending on your situation and requirements. Some individuals may take advantage of setting up more than one account because they need to separate their personal projects from their professional projects. Others may simply be open-source contributors and therefore require another account as you cannot use your managed user account to work on public projects on GitHub

Now, in terms of compliance, multiple accounts may support your compliance with organizational policies within your organization, as usually, your work account is for work-related purposes only. As a result, you can manage permissions effectively while keeping the personal and work-related repos separated but secure and access-restricted. 

Imagine if you had all of your personal and work-related repos under one account – it could raise a lot of issues. Another area where multiple GitHub accounts put you at an advantage is project management and collaboration. By separating different projects and using an account for a specific purpose, such as a work project with your team, you can easily manage project roles and notifications, as well as the aforementioned access controls. This way your work can be clear, organized, and collaborative. 

Types of GitHub accounts 

It is important to note that there are three types of GitHub accounts. These include user accounts, organization accounts, and enterprise accounts. Different types of accounts serve specific purposes and vary in terms of permissions. 

User accounts

When you sign into GitHub, you get a user account. Such an account can own repos, projects or packages. Also, any action you perform on GitHub – for example, a pull request, will be attributed to your user account. Although intended for humans, you can actually make accounts to automate tasks on GitHub. That would be called a machine user and could be used to automate continuous integration workflows. Furthermore, there are two different types of user accounts: 

  • A personal account is created when you sign up for your own account on GitHub.com. These use either GitHub Free or GitHub Pro plans. You can own an unlimited amount of public and private repos along with unlimited collaborators. Features vary depending on the chosen plan. When using multiple personal GitHub accounts they can be merged. 
  • A managed user account are created for you by an enterprise on GitHub Enterprise Cloud. Your account settings and details are partially managed by the enterprise. In order to access any organizations or repos you must sign in to the managed account. Now, you can create private repositories, however creating public content or contributing to any repos outside the enterprise is not possible. 

Organization accounts 

These are accounts where a number of people can collaborate on different projects at the same time. However, you can’t sign into an organization account, each individual has to log in using their user account and all actions made will be attributed to that account. There is no limit for user accounts on the number of organizations you can be a part of. In an organization, users can be granted different roles that define their levels of access controls. Only security managers and organization owners have permission to change the settings, admin features, and access controls. It’s also possible to create sub-groups called teams for members to simplify access management. 

With organizations, there is no limit on the amount of public or private repositories that can be owned. As a free option, you can use GitHub Free, but if you want a complete set of features such as the SAML single sign-on, there is GitHub Team or GitHub Enterprise Cloud. 

Enterprise accounts 

In terms of enterprise accounts, you get centralized management for multiple organizations. Admins for such accounts are able to manage enterprise membership and billing along with usage. Other permissions include security configurations, like SSO, listing the allowed IP addresses, 2FA, and SSH certificates. Administrators can also enforce policies and make use of GitHub Copilot along with Advanced Security. 

Authentication methods for GitHub accounts 

In terms of using multiple GitHub accounts, security is especially important. There must be a relevant way to authenticate users and access permissions. The majority of Version Control Systems platforms, like GitHub, support two primary methods of authentication: 

  • SSH – Secure Shell
  • HTTPS – Hypertext Transfer Protocol Secure

A few words about HTTPS 

When it comes to HTTPS you will mainly use it due to its simplicity and the fact that it’s built directly into the HTTP protocol. However, there is a catch – it requires you to input your credentials again every single time you try to clone, pull, or push to a repository (unless you are using a credential helper). This can be less secure, especially if you’re managing multiple accounts. 

Prioritize SSH for security and simplicity 

Now, let’s go over some details of SSH. Contrary to HTTPS, it is more secure and convenient. It uses a pair of cryptographically linked keys – a public and a private key. As we mentioned already, your public key will be shared and will encrypt relevant data, which can only be decrypted with your private key. Therefore, the key (pun intended) to maintaining security is to never reveal your private key and keep it secret as it is the only entity capable of decrypting your data.

This method is much simpler to use when compared to HTTPS because it doesn’t ask you to enter your credentials every time you push or pull from the repo, as authentication is handled through those keys. The way it works is that it is able to remember your identity through the ssh-agent – a program, which runs in the background and keeps your private key ready for continuous use. This approach can simplify your workflows and boost your cyber defenses by using cryptographic key-based authentication rather than well-known, traditional password-based methods. Therefore, if you use SSH keys you are more protected against cyber attacks and guarantee that your connections are always encrypted. 

To recap, while both HTTPS and SSH have their own purposes and uses, SSH is preferred for its security and convenience, especially for developers managing multiple GitHub accounts. It is important to understand the technical aspects of the possible authentication methods so that you can make an informed decision that will best suit your workflow and security needs. 

Set up multiple GitHub accounts with SSH for easier management

As we discussed, at some point in your developer journey you may face the need to set up and then manage more than one GitHub account. To set up your multiple GitHub accounts with SSH keys there are several steps you will need to follow. 

#1 Generating your SSH key

First of all, you will have to make sure that your current directory is in your .ssh folder, that is done through: 

  • ‘$ cd ~/.ssh’ 

Start off by generating a pair of SSH keys. You have two possible options to choose from, depending on the security and performance you need:

  • For ED25519 (recommended for greater security and performance): 

‘ssh-keygen -t ed25519 -C “[email protected]” -f ~/.ssh/descriptive_key_name’ 

  • For RSA (a valid alternative provided it is long enough): 

‘ssh-keygen -t rsa -b 4096 -C “[email protected]” -f ~/.ssh/descriptive_key_name’

Next, add a descriptive name and associated email to enhance manageability. The ‘-C’ option stands for comment (usually your email) to help you locate your SSH key, and ‘-f’ is the filename in which your key will be saved. 

#2 Securing your key with a passphrase 

Our next step is optional but it can improve your security. Adding a passphrase to your SSH key during creation is beneficial because if it gets compromised, the key remains protected unless someone has access to your passphrase. To update the passphrase use the command: 

‘ssh-keygen -p -f ~/.ssh/descriptive_key_name’

#3 Saving keys with ssh-agent 

To avoid having to enter your passphrase every time, you can use the ssh-agent to store your passphrase, the command will look like that:

‘eval “$(ssh-agent -s)” && \
ssh-add -K ~/.ssh/descriptive_key_name’ 

Keep in mind that the “-K” option which stores your passphrase in the keychain is only for macOS. 

#4 Craft your SSH config

Create a ‘.ssh/config’ file (unless you have one already). We set “Host” to “*” for all keys. This will allow Git to specify which SSH key to use based on different profiles: 

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<created_key> 

#5 Distributing your public key 

Copy your public key to your clipboard and add it to your GitHub organization

  • macOS: ‘tr -d ‘\n’ < ~/.ssh/descriptive_key_name.pub | pbcopy’
  • Linux: ‘xclip -sel clip < ~/.ssh/descriptive_key_name.pub’
  • Windows: ‘cat ~/.ssh/descriptive_key_name.pub | clip’

#6 Organizing your workspace 

Next, you should create a corresponding .gitconfig file within its workspace directory for each account to replace user settings with specific configurations. 

Assuming your directory looks like this: 

/myhome/
    |__.gitconfig
    |__work/
    |__personal/

You should create two different .gitconfig files for each of your directories, take a look: 

/myhome/
    |__.gitconfig
    |__work/
        |__.gitconfig.work
    |__personal/
        |__.gitconfig.pers 

#7 Customizing your Git configs 

Now, we will specify which SSH key Git should use for connecting to remote repositories. We clearly outline in Git which SSH key should be used for each project or a set of projects (work or personal) directly in Git configuration files. By doing so, the management of multiple accounts gets easier. Imagine if you have multiple GitHub accounts and they both use ‘github.com’ as the host – it may end up being overly complicated. If you were to manage different SSH keys in one single SSH config file you would have to create hostnames, such as “other.github.com”. Then, whenever you want to clone a repo you will have to use that other hostname and not “github.com”. As you can see this brings a couple of issues like the need to create and remember these arbitrary prefixes to make hostnames – which is not easy. Also, there is a threat of forgetting which hostname to use, which leads to even more mistakes and confusion. 

Take a look at how your config could potentially look like (the “-i” option is for specifying the identity file, which is your SSH key):

  • Work: 

# ~/work/.gitconfig.work
[user]
email = [email protected]
name = Your Name
[github]
user = “work_name”
[core]
sshCommand = “ssh -i ~/.ssh/<professional_key>”

  • Personal:

# ~/personal/.gitconfig.pers
[user]
email = [email protected]
name = Your Name
[github]
user = “myusername”
[core]
sshCommand = “ssh -i ~/.ssh/<personal_key>”

💡 This guarantees Git uses the correct SSH key for each of your accounts. 

#8 Implementing global configurations 

Next, you can take advantage of the global ‘.gitconfig’ to include common settings, such as your name. The ‘includeIf’ directive allows you to automatically apply different configurations based on the location of your directory. 

# ~/.gitconfig
[includeIf “gitdir:~/personal/”] # include for all .git projects under personnal/ 
path = ~/personal/.gitconfig-pers
[includeIf “gitdir:~/work/”]
path = ~/work/.gitconfig-work 
[core]
excludesfile = ~/.gitignore      # valid everywhere 

💡 Now, you can replicate this process for all of your accounts and enjoy the simplification that you have added to your workflow! 

Additional tips

Always double-check your configurations with ‘git config –list’ to guarantee that the correct settings are applied. 

Use the ‘ssh-keygen -l -f ~/.ssh/descriptive_key_name.pub’ command in order to verify if your public key has been correctly generated and added. 

What about multiple GitHub account management using CLI

As an option, you can use GitHub CLI for multiple account management. You need to download and install GitHub CLI on your device. Once that is done, you can set up and switch between multiple accounts in a streamlined way. Thus, you should:

  • Authenticate your GitHub accounts with ‘gh auth login’ and follow the authentication process. You must run this command for each of the accounts you want to manage. 
  • Switch between accounts using ‘gh auth switch [username]’. This could greatly simplify the process of switching accounts. Remember to replace “[username]” with the login credentials of the relevant account. 
  • To correctly set up Git configuration for each repository use ‘gh auth setup-git’. Based on the authenticated account, the aforementioned command will set up relevant Git configuration for your current repo. You may need to run  ‘gh auth setup-git’ in your repository directory, if your commits are not being attributed to the desired account. 
  • Clone repositories through the use of ‘gh repo clone repository_url’ command. Make sure to replace “repository_url” with the relevant URL. Since you can easily authenticate accounts and switch between them, it is rather simple to clone your repository (using the account that is currently active, or change to another one using ‘gh auth switch’). 
  • To verify which of the accounts is currently active run ‘gh auth status’. This way you will be able to see details regarding the currently authenticated account whenever you need to. 

Secure your GitHub accounts with automated backups 

One more thing – you should never forget about the security of your GitHub account. There are a lot of threats that your GitHub may face – cyber and ransomware attacks, vulnerabilities, outages, accidental deletions – you may learn about them in the State of DevOps Threats Report. How to secure your GitHub repository and metadata? Follow GitHub security best practices, including shifting security left, continuous vulnerability testing and scanning, and back up your GitHub account, etc.

GitProtect.io will provide you with backup best practices, including complete coverage of all your repos and metadata. Moreover, thanks to point-in-time restore and granular restore features, if you are affected by ransomware, hardware failures or unauthorized access leading to data loss, the downtime will be minimal and you will be able to quickly recover data to continue your workflow. Additionally, if your organization is in one of the regulated industries such as healthcare, government or financial services, then you are most likely obligated to keep backups (usually with long or even unlimited retention) of critical data for compliance purposes, for example for archiving. 

Summary 

Well, now you can take advantage of this simplified method to manage different GitHub accounts on one machine. Use the SSH keys to set up your multiple accounts, customize your configs and as a result, save yourself time and guarantee that you will not have to scratch your head to remember your hostnames every time you clone a repo. We advise you to complement your workflow with a robust backup solution to secure your GitHub environments, repos, accounts, and metadata for complete security!

[FREE TRIAL] Ensure compliant DevOps backup and recovery with a 14-day trial 🚀
[CUSTOM DEMO] Let’s talk about how backup & DR software for DevOps can help you mitigate the risks

Comments are closed.

You may also like