Skip to content

Instantly share code, notes, and snippets.

Last active Mar 19, 2019
What would you like to do?
Check if Git username is defined locally
# ## Description
# This pre-commit git hook makes sure that a local username is defined if there is any remote repository on github.
# This is helpful if you are working with multiple remote hosts and use different usernames. If you forget to configure
# the local username you may end up sending a wrong username to your github remote.
# ## Usage
# This is script is only useful if you make it available as a hook template. To achieve this, follow these steps:
# 1. Tell git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init or clone
# git config --global init.templatedir '~/.git-templates'
# 2. Create a directory to hold the global hooks
# mkdir -p ~/.git-templates/hooks
# 3. Move this script to `~/.git-templates/hooks/pre-commit`
# Now every time you do a `git init` or `git clone` git will copy this script to the repository and executes it
# before the commit is done.
RED='\033[0;31m' # red color
NC='\033[0m' # no color
GITHUB_REMOTE=$(git remote -v | grep
LOCAL_USERNAME=$(git config --local
if [ -n "$GITHUB_REMOTE" ] && [ -z "$LOCAL_USERNAME" ]; then
printf "\n${RED}ATTENTION: At least one Github remote repository is configured, but no local username. "
printf "Please define a local username that matches your Github account.${NC} [pre-commit hook]\n\n"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment