Skip to content

Instantly share code, notes, and snippets.

@dreness dreness/pre-commit
Last active Sep 24, 2019

What would you like to do?
#git pre-commit #hook to set commit email address based on the repo's remote domain(s)
# Set repo's to a default or alternate value based on remote domain.
# Because the in-flight commit info has already been set, if an email address
# change is needed, this commit is aborted. Retry the commit to use the new address.
# To automatically install custom hooks to new repos:
# 1) Add something like the following to ~/.gitconfig:
# [init]
# templatedir = ~/.git/templates
# 2) Create a directory at ~/.git/templates/hooks and install this file there
# Use 'git init' to add to existing repos
# If this repo's remote domain contains the string ALT_DOMAIN, set
# to ALT_EMAIL, otherwise use DEFAULT_EMAIL.
# Current repo's configured email address and remotes
EMAIL=$(git config --local
REMOTES="$(git remote -v)"
# If this repo has a local setting, do nothing. This allows for
# manually setting instead of using this script.
if [[ -n ${EMAIL} ]]; then
exit 0
if [[ "${REMOTES}" == *${ALT_DOMAIN}* ]]; then
# This repo has a remote in ALT_DOMAIN
echo "Setting to ${ALT_EMAIL}. Retry."
git config --local "${ALT_EMAIL}"
exit 1
# This repo does NOT have a remote in ALT_DOMAIN
echo "Setting to ${DEFAULT_EMAIL}. Retry"
git config --local "${DEFAULT_EMAIL}"
exit 1
printf "ERROR in pre-commit hook: %s\nCan't configure email address?! \n" ${SCRIPT_PATH}
exit 1
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.