Per-folder git configuration
My work consists of writing code for multiple clients and for some of them I need to use a different email in my git commits. I recently learned that there is a better approach for handling this than configuring email per repository.
During initial git setup you need to configure your name and email address to be used in git commits. Many graphical git clients provide user interface to do it, but you can also do it from command line:
git config --global user.name "Me"
git config --global user.email "email@primary.domain"
This configuration is saved to ~/.gitconfig in your home folder:
[user]
name = Me
email = email@primary.domain
By default, these settings are going to be used for all repositories you work with on your machine. You can however override any git configuration value for a specific repository. This includes your name and email. Some graphical git clients support this as well, but you can also do it by running the following commands from a folder inside that repository:
git config user.name "Me"
git config user.email "email@secondary.domain"
This configuration is saved to .git/config located at the repository root folder:
[user]
name = Me
email = email@secondary.domain
When alternative configuration is set for a repository like this, the name and email from it are going to be used for all commits to this repository.
Although this configuration process is simple enough, it doesn't scale well if your client uses microservices. It's too easy to forget changing the configuration for every single microservice repository you clone to your machine and start contributing to it. And in that case the wrong email from your default global configuration is going to be used.
Fortunately, you can use conditional includes to set the same configuration for all git repositories inside a specific parent folder. On my work machine, I already have all repositories for a certain client inside the same folder, so this matches well with my existing organization.
To use conditional includes, you first need to create a file with git configuration settings in standard format so that you can include it. I put it in the folder containing all the repositories from a specific client, but you could put it anywhere you like:
[user]
name = Me
email = email@secondary.domain
Then, you need to open your global git configuration file in ~/.gitconfig and conditionally include the configuration file you just created:
[includeIf "gitdir:~/Git/MyClient/"]
path = ~/Git/MyClient/.gitconfig
If a repository matches the path after gitdir, the configuration file specified after path will be evaluated and will override any configuration values defined earlier in your global configuration file. If you're in a case-insensitive file system, you might want to use gitdir/i instead of gitdir to make the path comparison case-insensitive.
For now, I'm only using conditional includes for user and email configuration when I need to change it for all repositories of a certain client. But it's a very flexible tool in the toolbelt, so I might find another use for it in the future.
