Skip to content

Instantly share code, notes, and snippets.

@BillyNate
Last active July 23, 2021 09:19
Show Gist options
  • Select an option

  • Save BillyNate/5b61c235378a4a172149904901bd6aa1 to your computer and use it in GitHub Desktop.

Select an option

Save BillyNate/5b61c235378a4a172149904901bd6aa1 to your computer and use it in GitHub Desktop.
Per git repository name/email config
  1. Make sure your git-templates directory is correctly set: git config --global init.templatedir "~/.git-templates"
  2. Create a .git-templates directory in your user directory (C:\Users\username on Windows)
  3. Add a subdirectory hooks
  4. Download the pre-commit file to the created directory

On every git init and git commit the hooks will be copied over to the new repository.
Whenever a git commit is issued, the pre-commit hook will check if the user.name and user.email are set, if not, it will ask for input.

#!/bin/sh
#
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
#
if [ "$(git config --get --local user.name)" == "" ]
then
while true; do
read -p "Please enter user.name for this repository ($(git config --get --global user.name)): " name
if [ "$name" != "" ]; then
git config --local user.name "$name"; break;
else
git config --local user.name "$(git config --get --global user.name)"; break;
fi
done
fi
if [ "$(git config --get --local user.email)" == "" ]
then
while true; do
read -p "Please enter user.email for this repository ($(git config --get --global user.email)): " email
if [ "$email" != "" ]; then
git config --local user.email "$email"; break;
else
git config --local user.email "$(git config --get --global user.email)"; break;
fi
done
fi
echo "Commit will be done by $(git config --get --local user.name) ($(git config --get --local user.email))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment