Problem
When maintaining multiple git repositories in the same project scope, it is cumbersome to have the same setting be applied to each of the .git/config files.
Assume the following directory tree:
~/.gitconfig
~/.ssh/customerA/x509.key
~/.ssh/customerA/x509.crt
~/customerA/project-1/sub-module-1
~/customerA/project-1/sub-module-2
~/customerA/project-2/module-1/
...
~/customerA/project-n/module/
~/customerB/project-1/
If you want to, e.g., share an X.509 certificate for authentication between all customerA projects, you would have to add the corresponding section to each of the .git/config files:
[http]
sslKey = ~/.ssh/customerA/x509.key
sslCert = ~/.ssh/customerA/x509.crt
Solution
Git provides the include and includeIf sections, which can be used in any Git config file, e.g. the .gitconfig in your home directory. Git applies an includeIf section with the gitdir: condition if the location of the repository's .git directory matches the configured pattern.
Instead of adding the [http] section to each .git/config file, you can add the following lines to your ~/.gitconfig file:
[includeIf "gitdir:~/customerA/"]
path = ~/customerA/customer-git-config
The trailing slash matters: if the pattern ends with /, Git automatically appends **, so every repository below ~/customerA matches. Without the trailing slash, gitdir:~/customerA would only match a repository whose .git directory is ~/customerA itself.
Add the [http] section to ~/customerA/customer-git-config and its settings are automatically applied to each Git repository below ~/customerA.
Notes
- You can also use the
onbranch:condition if you want to include a different config based upon the currently checked-out branch. includeIfinserts the content of the included file at the position of theincludeIfsection, so later settings override earlier ones. You can use multipleincludeIfsections.- Use
gitdir/i:for a case-insensitive match. Check which file a value comes from withgit config --show-origin --get http.sslCertinside a repository.
All files from this article, ready to copy and linted on every change: GitHub ↗