How to use multiple GitHub accounts on one machine

Return to posts

Post by Ozelot on 05/09/2024

Tags: GuideSysops

Intro

Managing multiple GitHub accounts on a single machine can be tricky. You may have one account for work and another for your lewd side projects, but you'd prefer to keep them entirely separate. Switching between accounts usually means setting Git configuration per every repository or updating credentials manually-tedious, right?

In this guide, I'll show you a better approach to managing multiple GitHub accounts using Git's configuration files, so you can seamlessly work across different repositories without manual changes. This tutorial focuses on Windows, but the method applies to Linux as well. I assume you have a basic understanding of Git.

Prepare your environment

Install Git

Download Git and install it. And if you already have Git, ensure it's in a fresh state or reconfigure it as necessary for this guide.

Create directories

To keep your repositories organized and properly configured, you'll need separate directories for each account's repositories. I suggest something like this:

Projects
├── Personal
└── Work

Personal account repositories will be located in Projects/Personal/ and work account repositories will be located in Projects/Work/. For this guide, we’ll assume the Projects directory is created on the C drive (C:\Projects).

Set up Git config

As you probably know, Git uses a global configuration file called .gitconfig to store details like your username and email. We need separate configuration files for each account. Let's create two configuration files: .gitconfig-personal and .gitconfig-work in the Projects folder.

Your directory should now look like this:

Projects
├── Personal
├── Work
├── .gitconfig-personal
└── .gitconfig-work

Sample configurations

Then configure them accordingly according to your needs. Here's an example of how each config file might look:

.gitconfig-personal:

[user]
    email = [email protected]
    name = LewdPuppyUwU
[init]
    defaultBranch = main
[credential]
    username = lewdpuppyuwu # e.g. your github username

.gitconfig-work:

[user]
    email = [email protected]
    name = John Doe
[init]
    defaultBranch = main
[credential]
    username = johndoe # e.g. your github username

In the credential section, replace the username with your respective GitHub username or anything unique - it will be used to recognize your account on the local machine.

Modify the global Git config

But that's not all. Next, we need to instruct Git to use these custom configuration files based on the directory where your repositories are located. This is done by modifying the main .gitconfig file, typically located at %USERPROFILE%/.gitconfig (on Windows). If this file doesn't exist, create it.

Here's the configuration of what your file should look like:

[includeIf "gitdir:C:/Projects/Personal/"]
    path = C:/Projects/.gitconfig-personal
    
[includeIf "gitdir:C:/Projects/Work/"]
    path = C:/Projects/.gitconfig-work

Make sure to update the paths if you're using a different directory structure. This setup ensures that Git automatically switches between the personal and work configurations based on where the repository is located.

Such proper configuration will cause that for repositories located in Projects/Personal/ a .gitconfig-personal will be used, which points to the personal account. For Projects/Work/ similarly .gitconfig-work will be used.

Let's test it

To test your configuration:

  1. Create a private repository on both GitHub accounts for testing purposes.
  2. Go to Projects/Personal/ directory and clone your personal repository.
  3. When prompted, log in with your personal GitHub account. Once authenticated, you can commit and push changes as usual.
  4. Now, repeat the process in the Projects/Work/ directory for your work account.
  5. Again, log in with your work account when prompted.

Now you should be able to work with repositories under Projects/Personal/ using your personal account and those under Projects/Work/ using your work account without needing to switch credentials manually.

Conclusion

Managing multiple GitHub accounts on a single machine doesn't have to be a hassle. By setting up separate Git configuration files for each account and organizing your repositories smartly, you can seamlessly switch between different profiles. Whether you're working on personal projects or company repositories, Git will always use the right credentials.