1 min read

Managing Git Configurations with Conditional Includes

I personally came across a situation where I used a default email address for my commits for any projects. However, I want to use a different email address whose custom domain name belongs to an organisation. With the default settings, and while creating new directories, I tend to forget to change the git config before committing any changes. This can be particularly frustrating, especially after multiple commits and having pushed to the remote repository.

Since Git v2.13, they have added the conditional includes feature. This allowed us to set the git config for specific conditions.

Take for example the directories setup.

Any projects: $HOME/projects Organisation's projects: $HOME/projects/organisation

  email = john.doe@personal.dev
  name = John Doe
  signingkey = SomeSigningKey
  gpgSign = true
[includeIf "gitdir:~/projects/organisation/"]
  path = ~/.organisation.gitconfig

So what this does is to set a default global configuration and the includeIf will set the specific configuration for the organisation for the particular directory. This will also work for any subdirectories too.

Hope this helps anyone who came across similar situations and were looking for a seamless solution to this.