blog::weyl.io

Chris Weyl's Technical Blog

Conditional git Configuration

git has always(?) allowed for the additional configuration files to be unconditionally included:

[include]
    path = path/to/gitconfig

Each individual git repo has always had the ability to maintain its own configuration at .git/config. However, sometimes on our systems we also have certain locations where we store multiple git projects, which may need different configuration from the global, but still common across that location.

Since ... well, for the last year or two at least, git has allowed for the conditional inclusion of configuration files.

For example, I contribute to F/OSS projects using one email address, which lives in my global git config (~/.config/git/config, in my case). However, for work projects, I want to use my work email everywhere — and accidentally pushing w/my personal email address is just embarrassing. All of my work projects live under a certain directory, so I can tell git that if a given repository's gitdir lives under ~/work, it should also load an additional configuration file:

[includeIf "gitdir:~/work/"]
    path = ~/work/gitconfig

...and in there, I can set

; this is ~/work/gitconfig
[user]
    email = cweyl@work.com

In this way, I do not need to remember to change the email address of any repos I clone under ~/work to my work address. This is especially useful as I not infrequently find myself forking and submitting bugfix PR/MR's to upstream, and if I do that for $work then I want to be using my work email address.

See also the "Includes" and "Conditional Includes" sections of the git-config manpage.