Conditionally setting gitconfig

For years, I've used this alias in my gitconfig:

work = !"git config '[email protected]'"

Whenever starting a new work project, I would run git work to set a local gitconfig for the project, and it worked well. However, since I don't frequently clone repositories at work, I would forgot to run it.

During a recent dotfiles cleanup, I wondered if there was a better way to handle different settings per project. It turns out there is! You can conditionally load configurations in your gitconfig.

In your gitconfig:

[includeIf "gitdir:~/Projects/Work/"]
path = ~/.config/git/work

And in ~/.config/git/work:

name = Rich Grundy
email = [email protected]

gpgsign = true

This allows you to conditionally target a directory to load extra config options, which is convenient as all my work projects reside in one directory.

There are several other options available. Aside from targeting directories, you can also target the remote:

[includeIf "hasconfig:remote.*.url:[email protected]:OrgAccount/**"]
path = ~/.config/git/work

Hope that helps. 😉