Skip to main content Lil Ramblings

Condition Git Config

So I’m a fan of making my personal projects be personal and my work ones be, well…work - so I’m not a big fan of my work email popping up in my git log, and vice versa. This mainly becomes an issue when I’m working on little “in my own time” projects, whether for work or not - and it’s been something that bothered me for a while (until I forgot about it). And now, it’s started to bother me again (it’s the little things…procrastination go brrrrr) - so I searched for a solution.

My initial (and I mean initial 0 research) thought was to stop bothering with a global git config and make local, repository-based config changes. But then I found out that multiple configurations are natively supported in git1:

You can conditionally include a config file from another by setting an includeIf..path variable to the name of the file to be included.

So this allows including another gitconfig file based on the path of the target git directory (at least my use case is this - it can be as granular as the branch). So this gets even better - since it’s separate gitconfig files, it means that everything can be different - so finally I can swap to SSH clone for everything rather than SSH for work/personal and HTTPS for the other. Anyway - to make this change, it’s just three steps:

  1. Copy your old .gitconfig twice - for me I named a work and personal one
  2. Make any changes to either of the work or personal gitconfig files as is needed.
  3. Update the original .gitconfig with the following (_of course, updating the specified gitdir and path values)

code snippet start

$ cat -p ~/.gitconfig
[includeIf "gitdir:~/workgit/"]
    path = .gitconfig_work
[includeIf "gitdir:~/l33tpersonalprojects/"]
    path = .gitconfig_personal

code snippet end

And just like that, I have no reason to procrastinate (for now).