Git config: Separation for work & personal repositories
Introduction
Developers often use a single computer for multiple purposes, such as work and personal projects. Some go as far as separating their GitHub profiles entirely, while others prefer to use one account for everything.
Personally, I’ve always used the same GitHub profile and the same Git config for both work and personal projects when working on the same computer. At least, I did until I started fresh recently!
It started annoying me when I used the wrong user details for commits—such as using my work email for a personal hobby project, or forgetting to use my GPG key to sign commits on personal repos. This logic can apply to almost anything you want to differentiate between environments.
Git config to the rescue! This annoyance is extremely simple to resolve by applying a few small config tweaks. If you haven't recently, I highly recommend visiting Git's "First-Time Git Setup" guide. Even if you are already familiar with Git, it contains some very useful gems.
Using different configs between repositories
Using simple directory glob pattern matching, I separated my configs for two different purposes: Work and Personal (the latter being hobby projects and open-source). To apply different configurations, I use the following files:
.gitconfig: The default global Git config. Since the majority of my work relates to my job (and they technically own the computer), it makes sense for this to be the default..gitconfig_personal: A specific file that overrides the email and signing key of my work profile based on the parent directory of the repository.
The Git config structure
The Git config is structured in sections defined by brackets, like [section_name]. For example, [user] is used for global user configs. We will use this for our default (work) profile:
[user]
name = Daniel Hillmann
email = daniel@myworkplace.com
signingKey = ABC123
Great! Now all commits will be made with this information by default. But what about the profile for personal repositories? To apply different configs for personal projects, we utilize the includeIf section (see docs). This is a conditional that lets us include config directives from another source using the path variable if the condition is met. We define the includeIf section with the condition, followed by the path to the target config:
[includeIf "gitdir:<directory to use>"]
path = <path of the personal profile>
For example, if I store all my personal repositories in a directory named personal:
[includeIf "gitdir:~/personal/**"]
path = .gitconfig_personal
Finally, we configure .gitconfig_personal just like we did with the base .gitconfig, but with different information:
[user]
email = daniel@hillmann.com
signingKey = QWE987
You may have noticed that only the base config defines the name section. This is because I use the same name for both profiles; anything you don't explicitly override in the conditional config will remain as defined in the default config.
That’s it! You now have multiple Git configs that will automatically swap based on which repository you are working in.
This is just an example of my specific use-case. It can be used in unlimited different ways fine-tuned to your specific needs.
Pro Tip: How to check if it's working
Once you've set this up, you'll want to make sure Git is actually swapping your profile correctly. You don't need to make a "test commit" to find out!
Simply open your terminal, move into a folder, and ask Git who you are:
- Go to a work folder and run:
git config user.email.- It should show your work email.
- Go to your personal folder and run the same command:
git config user.email- It should show your personal email instead.