The solution I came up with is inspired from Scott Weldon's answer. Since it was not directly applicable for my case, I adapted the hook's bash script and improved several parts*.
Assume the following directory structure from the home directory:
~
.gitconfig // [init] templatedir
.git-template
hooks
post-checkout // our bash script
MyDomain
.gitconfig // [user] name, email
Initially I let Git know where my template directory is. On Windows, you may need to specify the absolute path instead (C:/Users/MyUser/.git-template
).
git config --global init.templatedir '~/.git-template'
In ~/MyDomain/.gitconfig
I store the configuration for that directory (domain), which should be applied to all repositories inside it and its subdirectories.
cd ~/MyDomain
git config --file .gitconfig user.name "My Name"
git config --file .gitconfig user.email "my@email.com"
The interesting part is the post-checkout
bash script, which defines the post-checkout hook. I used a custom user.inferredConfig
flag to execute it only once (on git clone
), not repeatedly (on git checkout
). It would of course also be possible to create a separate file to represent that state.
#!/bin/bash
# Git post-checkout hook for automated use of directory-local git config
# https://stackoverflow.com/a/40450106
# Check for custom git-config flag, to execute hook only once on clone, not repeatedly on every checkout
if grep -q "inferredConfig" .git/config
then
exit
fi
# Automatically set Git config values from parent folders.
echo "Infer Git configuration from directory..."
# Go upwards in directory hierarchy, examine all .gitconfig files we find
# Allows to have multiple nested .gitconfig files with different scopes
dir=$(pwd)
configFiles=()
while [ "$dir" != "/" ]
do
# Skip first directory (the newly created Git repo)
dir=$(dirname "$dir")
if [ -f "$dir/.gitconfig" ]
then
configFiles+=("$dir/.gitconfig")
fi
done
# Iterate through configFiles array in reverse order, so that more local configurations override parent ones
for (( index=${#configFiles[@]}-1 ; index>=0 ; index-- )) ; do
gitconfig="${configFiles[index]}"
echo "* From $gitconfig:"
# Iterate over each line in found .gitconfig file
output=$(git config --file "$gitconfig" --list)
while IFS= read -r line
do
# Split line into two parts, separated by '='
IFS='=' read key localValue <<< "$line"
# For values that differ from the parent Git configuration, adjust the local one
parentValue=$(git config $key)
if [ "$parentValue" != "$localValue" ]
then
echo " * $key: $localValue"
git config "$key" "$localValue"
fi
done <<< "$output"
# Set custom flag that we have inferred the configuration, so that future checkouts don't need to do it
git config user.inferredConfig 1
done
*: The changes from the original code include:
- Works with spaces in paths (especially interesting on Windows)
- Parses key-value pairs from
.gitconfig
correctly (don't read lines with for
, iterate with while read
instead)
- Checks
.gitconfig
files from root to local directory, not vice-versa
- Invokes hook only at initial clone, not at every checkout
- Output the config settings that are applied on
git clone