Scott's Weblog The weblog of an IT pro focusing on cloud computing, Kubernetes, Linux, containers, and networking

Conditional Git Configuration

Building on the earlier article on automatically transforming Git URLs, I’m back with another article on a (potentially powerful) feature of Git—the ability to conditionally include Git configuration files. This means you can configure Git to be configured (and behave) differently based on certain conditions, simply by including or not including Git configuration files. Let’s look at a pretty straightforward example taken from my own workflow.

Here’s a configuration stanza from my own system-wide Git configuration:

[includeIf "gitdir:~/Work/Code/Repos/"]
    path = ~/Work/Code/Repos/.gitconfig

The key here is the includeIf keyword. In this case, Git will include the referenced configuration file specified by path, if the location of the Git repository matches the path specification after gitdir. Basically, what this means is that all repositories under ~/Work/Code/Repos will trigger the inclusion of the additional configuration file.

Here’s the additional configuration file:

[user]
    email = name@work-domain.com
    name = Scott Lowe
[commit]
    gpgsign = false

As long as I group all work-related repositories in the specified directory path, these values override the system-wide values. This means I can specify my work e-mail address as the e-mail address to be associated with commits to work-related repositories while all others use a different e-mail address. This configuration also allows me to disable GPG signing of commits for work-related repositories (i.e., repositories in the specified path), since I don’t have a GPG key associated with my work e-mail address.

Could you do this with per-repository configuration settings? Absolutely. This configuration mechanism allows you to apply configuration settings to groups of repositories based on their filesystem location, instead of having to do the same thing on a per-repository basis.

I hope you find this information useful. Do feel free to hit me up—on the Fediverse, on Twitter, or in any of a variety of Slack communities—if you have any questions or any feedback!

Metadata and Navigation

Be social and share this post!