1 min read
·
0 views

Conditional settings for .gitconfig

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 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

.gitconfig

_10
[user]
_10
email = john.doe@personal.dev
_10
name = John Doe
_10
signingkey = SomeSigningKey
_10
[commit]
_10
gpgSign = true
_10
[includeIf "gitdir:~/projects/organisation/"]
_10
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.