TIL you can have conditional sections in your .gitconfig
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 [email protected]
.
Where my config looks something like this:
[user]
email = [email protected]
name = Haseeb Majid
# ...
However, when I am at work I need to use a different email to commit i.e. work email [email protected]
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 protected]
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. /home/
vs /Users/
.
Then in the .gitconfig.mac
we can do something like:
[user]
email = [email protected]
This will replace the email with my work one when I commit when we run the git
CLI command in a folder within /Users/
folder.
Wild Card
When we specify/Users/
in the gitdir
if 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.