How to Manage Multiple Git Profiles in the same PC
If you're juggling multiple Git (GitHub/GitLab) accounts on a single machine and seeking a seamless way to manage them, you've come to the right place. While most solutions online involve configuring SSH files and custom hosts for each user, these methods can be complex and intimidating, especially if you're not familiar with SSH or encountering issues along the way. Fear not, because I've got a simpler solution for you, outlined in just four easy steps.
Disclaimer: This solution utilizes the HTTPS protocol for interacting with your repositories. If you prefer SSH for security reasons or personal preference, you may refer to alternative methods.
The Solution
Since version 2.13, Git has provided the capability to include config directives from external sources. This feature enables us to use different Git configurations for each repository based on its directory location within your computer. We'll leverage this functionality to set up 2 user profiles in this article, but the idea can be extended to any number of profiles.
Step 0 - Understanding .gitconfig
Firstly, locate your global .gitconfig file. This file stores crucial Git configurations such as user credentials, aliases, and default text editor settings. If you're using Git, you likely already have this file. If not, you can download and install Git from here.
UNIX-based OS: Typically located at ~/.gitconfig
Windows: Typically located at C:\Users\username\.gitconfig
Step 1 - Organize Your Workspaces
To maintain clarity and organization, create distinct folder structures to house your repositories. Consider creating a master directory for each user profile and store your repositories there. For example, you could dedicate entire volumes (e.g., D:\ and E:\) as separate workspaces like me, or organize them within a single volume as per your preference.
Step 2 - Create Local Git Configurations
Next, create a .gitconfig file for each user profile you intend to use. Place them in the root of the directory of each workspace.
Work
# D:/.gitconfig
[credential]
username = <work-username>
[user]
name = <Account Name>
email = email@company.com
Personal
# E:/.gitconfig
[credential]
username = <personal-username>
[user]
name = <personal-username>
email = <personal-username>@users.noreply.github.com
Step 3 - Update Global Git Config
Finally, update your global .gitconfig file to incorporate conditional includes, allowing Git to pick up the appropriate user profile configuration based on the repository's location.
# C:/Users/username/.gitconfig
[includeIf "gitdir/i:D:/"]
path = D:/.gitconfig
[includeIf "gitdir/i:E:/"]
path = E:/.gitconfig
For UNIX OS users, it would look something like below.
# ~/.gitconfig
[includeIf "gitdir/i:~/projects/work/"]
path = ~/projects/work/.gitconfig
[includeIf "gitdir/i:~/projects/personal/"]
path = ~/projects/personal/.gitconfig
Note: You may still keep your global user credentials in the global .gitconfig. These settings will automatically apply to repositories that do not adhere to the path pattern(s) defined in the conditional includes. Ensure that you place the user and credential before the conditional includes in the .gitconfig to avoid unexpected behaviour.
That's it! Now you can seamlessly use multiple user profiles to work with your repositories without worrying if the right user is interacting with them.
Happy coding!