Custom git config per directory tree
- Julius Zeidler
- ,
- February 24, 2024
I usually am allowed to use my work computer privately as well, so often I find myself coding away on a sideproject in the evening without switching to another machine. Or I found a really cool thing I wanted to try out.
More than once I accidentally committed to a private project using the wrong Git config, so my work email was in those commits. Not really cool and I always tried to make sure to have a custom git config per private project.
However, today I’ve learnt: You can specify a git config per directory tree. Means: My work code gets my work config and my private stuff gets my private config. And if I want to code some hamradio stuff, I can have another ID that shows my callsign rather than my name. Or if I code on my super secret projects I do want to have affiliated with my alter ego (I really don’t have one, this is all hypothetical, or is it?) that has no ties to my real name.
The method really is straight forward. The following lines in your ~/.gitconfig have you covered:
[includeIf "gitdir:~/work/dev/"]
path = ~/.config/gitconfig-workstuff
[includeIf "gitdir:~/private/dev/"]
path = ~/.config/gitconfig-private
[includeIf "gitdir:~/alterego/dev/"]
path = ~/.config/gitconfig-s3cr3tsq41rr37
Go and watch the great “So you think you know git” video

