DEV Community

Jeremy
Jeremy

Posted on

Override Git config based on project path

Recently I got a new laptop from my employer that I planned to use for personal projects also.

The problem that I faced, was that I didn't want my personal email to appear on company projects commit history, nor my professional email on personal projects.

I could have set my professional email globally and then override it on each of my personal projects. But I am too lazy to do it, and I know that sometimes in the future I will forget to set my personal email in a new project.

As I take the habit to regroup all personal project under the same folder, I wanted to put a .gitconfig file in this directory and have it load by Git when I was inside, but it didn't work because Git is only looking for configuration locally (.git/config) or globally (`~/.gitconfig).

Finally I found a solution, thanks to a StackOverflow, that since Git version 2.13 it is possible to use includeIf directive to conditionally include configuration files.

The includeIf directive accept the two following criteria

  • gitdir: to specify a glob pattern that will be compared to a project .git folder path
  • branch: to specify a branch that will be compared to a project current branch

When the criteria are met, it includes the file given with the path parameter

Whis this new information I was able to set automatically my email for all my personal projects by putting these 2 lines in my ~/.gitconfig file.

Links

Top comments (0)