TIL you can have conditional sections in your
As some of you may know, I keep my dotfiles in a git repo.
The problem with this approach is that my email in my
.gitconfig is set to
Where my config looks something like this:
[user] email = email@example.com name = Haseeb Majid # ...
However, when I am at work I need to use a different email to commit i.e. work email
firstname.lastname@example.org not my personal
email. So I was wondering how could I do that; one way would be to change the file locally and just not commit it.
But of course, that is not ideal.
Then I discovered we can add other gitconfigs using the conditional
includeIf clause. You can
read more about them here.
[user] email = email@example.com name = Haseeb Majid [includeIf "gitdir:/Users/"] path = .gitconfig.mac # ...
I use a Mac at work and Linux at home so I can a basic check on folder structure i.e.
Then in the
.gitconfig.mac we can do something like:
[user] email = firstname.lastname@example.org
This will replace the email with my work one when I commit when we run the
git CLI command in a folder within
Wild CardWhen we specify
gitdirif clause it automatically treats it like
/Users/**. So any sub-directories within users also count.
That’s it! Now we can keep separate settings between work and home. We can also extend this to have different settings between different OS’s such as default editor.