If you are working for an agency or for your own clients, but you also work on personal projects, you might want to use different Git settings for these two types of projects. When setting up your machine to work on projects, you configure some basic tools like Git and GitHub. Those configuration settings do include your Git author email address and maybe the associated SSH/GPG key to sign commits. You can normally only define one global author email address, so you might just use your professional email address (from your employer or your own freelancer address). If you also work on personal projects or contribute to open source projects, you might prefer to use your personal GitHub email address instead. This guide will help you to set this up.
In this guide, we assume that you usually work with PhpStorm and store all projects you are working on in the
~/PhpstormProjects folder. But the same approach would also work with any other IDE and folder structure.
In order to be able to use two different email addresses with GitHub, you have to verify them first. Then you can select the best one any time you execute something on github.com, like merging a PR. Having those email addresses verified is also essential to be able to sign commits with these addresses.
Splitting up your configuration files and conditionally load them
Usually, you have all your global Git configurations in the file
~/.gitconfig. The file could look like this:
[user] name = Jo Doe email = [email protected] signingkey = ~/.ssh/id_ed25519.pub
Now, in any project you make commits, you would your company.com email address and sign it for this address. If you’d want to use a different email address, you would have to add this manually when doing the commit:
git commit -m"message" --author="Jo Doe <[email protected]>" --gpg-sign=~/.ssh/id_ed25519.pub
This is not really practical, and you would probably forget to add those arguments sometimes. Alternatively, you could add the author settings to the project-specific configuration file, but you would also have to do that for every single cloned repository.
While it would also be technically possible to change the author afterwards, it’s far from easy and requires an interactive rebase which rewrites the history and must not be done with pushed commits. So how can it be achieved differently?
Load configuration files conditionally
Within a Git configuration file, you can use includeIf to load the file conditionally. The easiest way to do that is by having different folders for your projects. We would store personal and open source projects in
~/PhpstormProjects and company projects in the sub-folder ~/PhpstormProjects/company. We can then add the following to our global Git configuration:
# file: ~/.gitconfig [includeIf "gitdir:~/PhpstormProjects/"] path = .gitconfig-general [includeIf "gitdir:~/PhpstormProjects/company/"] path = .gitconfig-company
Then we would move all specific settings into these files. This would probably be the whole
[user] settings as well as some other ones. The general file might look like this:
# file: ~/.gitconfig-general [user] name = Jo Doe email = 1234567+jo.d[email protected] signingkey = ~/.ssh/id_ed25519.pub
In this code snippet, I am using the GitHub “noreply” email address you can set up instead of your real personal email address to keep it private and prevent it from being used to send you spam.
Now, for the company sub-folder, we use this separate configuration file.
# file: ~/.gitconfig-company [user] name = Jo Doe email = [email protected] signingkey = ~/.ssh/id_ed25519_company.pub
Those two files are stored in your home directory alongside the global
.gitconfig file. If you want to overwrite more settings conditionally, just add them to these files.
On the Git documentation about includes, you can even learn how to conditionally load files due to different things than the
gitdir. You could for example also load config files based on the remote URL, so even if you clone a company repository to a folder outside
~/PhpstormProjects/company/ it would conditionally load the
.gitconfig-company file. But it’s probably easier to understand which configuration is loaded based on the parent folder.
Bonus: Managing GitHub notifications for multiple email addresses
When you get invited to the github.com organization of your agency, you are usually automatically subscribed to notifications for all repositories. As you have probably used GitHub only for private projects before, you will receive all these notifications to your private email address. Fortunately, you can route emails for notifications based on the organization.
To define those routing rules, navigate to “Settings | Notifications”. Here you click on the “Custom Routing” Button and then on the “Add new route” button. Then pick the organization, select the email address and click “Save”. If you have been invited to multiple organizations, you can add multiple rules.
If you are using gitlab.com for your company/personal work, you can also set a notification email per group or even projects in your profile in the “User Settings | Notifications” setting.
Working on company and private projects at the same time on one device can really mess up the Git author data in your commits. But with the conditional loading of configuration files, you can use settings specifically for different types of projects.