This post has been written from the perspective of a macOS user.
Working for an agency has its perks. Diverse & interesting people, less bullshit & more doing, working for different clients, and so on. Especially the “working for different clients” part can have its challenges, though.
One is that with some clients, we need to use external email addresses provided by them. Not just for communication but also for more technical things like logins and Git commits.
I want to show you a neat trick to manage your different configurations for Git easily and comfortably.
Git manages some meta information in the global
.gitconfig. Here’s mine:
The global user configuration is under
[user] and sets the name and email for Git. This will be applied to all my Git projects if there’s no local configuration, which overrides it.
Let’s imagine that with the fictional company “acme labs”, we would be required to use external accounts provided by the client. If you’re not careful, you will end up with commits like this:
You probably know that there is a global Git configuration that will be applied to all your projects if not overridden by a local configuration.
It resides under
~/.gitconfig. You can set its values by editing this file or by the
git config command with the
For your user and email, that would be:
git config --global add user.name “Stefan Antal”
git config --global add user.email “Stefan.Antal@sinnerschrader.com”
In this way, you can also set local Git configuration options by removing the
Switch to your project folder (for me this would be
~/code/acme-labs) where you have your
.git folder and run the commands without the
git config add user.name “Stefan Antal (external)”
git config add user.email “Stefan.Antal@external.acme.labs”
This will add your project-specific user and email to
All values present in this file will override its global counterpart for this project.
This is all perfectly fine if that’s all you need.
But what if you would like to reuse some values multiple times?
You can include files from all kinds of places into your Git configuration. I like to gather my different configuration files in a common-place like
Let’s create a file that we call
acme-labs (the name doesn’t matter) inside our
I create a config file for each project which needs to have a separate configuration.
The content of
acme-labs will be this:
Now we need to tell Git to use this configuration when we’re working for acme-labs.
To achieve this, we’ll use the
includeif option inside our global Git configuration.
Modify your global Git configuration (
~/.gitconfig) by adding following content:
[includeIf "gitdir:~/code/acme-labs/"] path = ~/git-configs/acme-labs
gitdir shall point to the folder where your local
.git folder is.
path shall point to your newly created Git configuration for acme-labs.
The result should look like this:
Git will recognize when you’re working inside
~/code/acme-labs and use the values you defined under
In this way, you only have to add a new
includeif per project inside your global Git configuration and link it to one of your configuration files.
Your next commit will now look like this:
However, if you also have regular local Git configuration, which you have added with
git config add, it will be used instead of your