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 a 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 section, which can be applied to the .gitconfig in your home directory. includeIf is applied by the git, if the current working directory matches the configured path.

Instead of adding the [http] section to each .git/config file, you can use the following line in your ~/.gitconfig file:

[includeIf "gitdir:~/customerA/**/**"]
path = ~/customerA/customer-git-config

Add the [http] section to ~/customerA/customer-git-config and all git settings below ~/customerA are automatically applied to each git repository.

Notes

  • You can also use the onbranch: argument, if you want to include a different config based upon the active branch of the current git project.
  • includeIf inserts the content of the included file immediately. You can use mulitple includeIf statements.