Skip to content

Instantly share code, notes, and snippets.

@a-kinder
Last active August 30, 2018 17:57
Show Gist options
  • Save a-kinder/20475721f891fcf12f98ff962cff25e5 to your computer and use it in GitHub Desktop.
Save a-kinder/20475721f891fcf12f98ff962cff25e5 to your computer and use it in GitHub Desktop.
Blocks commits to master unless `-n` flag is provided
#!/bin/bash
# Stops accidental commits to master. Adapted from https://gist.github.com/ColCh/9d48693276aac50cac37a9fce23f9bda
# Install in project:
# install or symlink at .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
echo -en "\033[31mCommit to master? [y|n] \033[0m"
echo -en "\033[1m"
read -n 1 -r < /dev/tty
echo -en "\033[0m"
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment