Skip to content

Instantly share code, notes, and snippets.

What would you like to do? GIT Directory-Based Configs


So, you are a proud OpenSource developer and work for a company using for GIT repository hosting?

While SSH auth to is based on public keys and it automatically chooses the right account you have to take care when you quickly clone a repository, change something and whoops your company has your private email address or hacker pseudo within their repository.

The scenario we are speaking about looks like following:


    acme/            ← use acme.config here with email="", name="John Barleycorn"

    foss/            ← use foss.config here with email="", name="Acid Burn"

Here we are using GIT’s global hooks and include.path to setup repository-specific configs.

This guide is aimed at Linux users but should be able to be used on Unix (OSX) or Windows as well.


Default Config Path

We're using a temporary config path here following FreeDesktop specifications. This is just used for the copy-and-paste in this tutorial and should not be put somewhere in a profile.


Setup Example Configs

mkdir -p $CONFIG_PATH/config
echo -e "[user]\" > $CONFIG_PATH/foss.config
echo -e "[user]\" > $CONFIG_PATH/acme.config

Setup global hooks directory

mkdir -p $CONFIG_PATH/hooks
git config --global --replace-all core.hooksPath $CONFIG_PATH/hooks

Create Pre Commit Hook

Create $CONFIG_PATH/hooks/pre-commit and don’t forget to chmod a+x $CONFIG_PATH/hooks/pre-commit!

The lines with acme and foss will do the matching. Customize for the companies you're working for! Also keep in mind that include.path might get overwritten and if no match takes please git will use the global config!


# Config directory
CONFIG_PATH="$( cd "$(dirname ${BASH_SOURCE[-1]})/.." && pwd)"

# default config

# Git Hooks documentation

# If we have a local commit hook, execute it
localHook="${PWD}/.git/hooks/$(basename "${0}")"
if [[ -e  "${localHook}" ]]; then
	echo "Executing local hook"
	$localHook "$@" || exit $?

echo "Executing global hook"

case "${PWD}" in
	*acme*) GIT_INCLUDE_CONFIG="acme.config";;
	*foss*) GIT_INCLUDE_CONFIG="foss.config";;

currentConfig=$(git config --local include.path || true)
if [[ -n "${currentConfig}" ]] && [[ ! -e "${currentConfig}" ]]; then
	echo "${currentConfig} does not exist! Fix or remove by git config --local --replace-all include.path \"\"" >&2
	exit 1
# If we have a config to be applied
if [[ -n "${CONFIG_PATH}/${GIT_INCLUDE_CONFIG}" ]]; then
	if [[ ! -e "${CONFIG_PATH}/${GIT_INCLUDE_CONFIG}" ]]; then
		echo "${CONFIG_PATH}/${currentConfig} does not exist! Please fix ${BASH_SOURCE[-1]}!" >&2
		exit 1
	if [[ "${currentConfig}" != "${CONFIG_PATH}/${GIT_INCLUDE_CONFIG}" ]]; then
		echo "Applying config: ${CONFIG_PATH}/${GIT_INCLUDE_CONFIG}"
		git config --local --replace-all include.path "${CONFIG_PATH}/${GIT_INCLUDE_CONFIG}"


Create temporary directory and empty repository

TMP=$(mktemp -d) && cd $TMP
mkdir acme && cd acme
git init

Create a File with Random Content, Add and Commit it

echo "$RANDOM" >
git add
git commit -m "Initial Commit"

Now within the acme-directory this should read:

git add
git commit -m "Initial Commit"
Executing global hook
Applying config: /home/ctang/.config/git/acme.config
[master (root-commit) 3931374] Initial Commit
 1 file changed, 1 insertion(+)
 create mode 100644

The should now read from the acme.config

$ git config --get

The can be locally overwritten git config --replace-all "" and reset by git config --unset (in repo.fodler)


Using ZSH and directory based config

With ZSH you could also take advantage of the GIT_CONFIG environment variable when on directory change. If you are using bash you should change to a better shell ;) It’s much less of a hassle and a much cleaner solution for the problem.

chpwd_git() {
  local configPath="$HOME/.config/git"
  case "${PWD}" in
    *acme*) export GIT_CONFIG="${configPath}/acme.config";;
    *foss*) export GIT_CONFIG="${configPath}/foss.config";;
chpwd_functions=("${chpwd_functions[@]}" "chpwd_git")

GIT Global Templates

If a global hook exists local hooks will be ignored. Therefore we check for a local hook in the script and execute it as well.

Previous to GIT version 2 GIT was using Templates which still can be used to initialize repositories. But they are just copied when initailizing repositories. You can setup them as well. Note that the hooks path will be $CONFIG_PATH/templates/hooks!

git config --global --replace-all init.templatedir $CONFIG_PATH/templates

Use commit hooks. They probably make your life as developer easier ;)



This comment has been minimized.

Copy link
Owner Author

commented Feb 8, 2019


This comment has been minimized.

Copy link
Owner Author

commented Feb 12, 2019

Note: If using zsh/GIT_CONFIG solution please move your ~/.gitconfig to .config/git/config and everything will be fine again when using git config --global.

Also it's possible to add global ignore like with
git config --global core.excludesfile "$HOME/.config/git/ignore"


This comment has been minimized.

Copy link
Owner Author

commented Feb 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.