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.
The Situation
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:
The easy Fix
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 --global
option.
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 --global
flag.
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 --global
flag:
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 .git/config
.
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?
The sophisticated Fix
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 ~/git-configs/
.
Let’s create a file that we call acme-labs
(the name doesn’t matter) inside our git-configs
folder.
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 ~/git-configs/acme-labs
.
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 ~/code/acme-labs
configuration.
Top comments (0)