Skip to content

Instantly share code, notes, and snippets.

@sa-mm
Forked from a-kinder/master-commit-check
Last active August 30, 2018 18:03
Show Gist options
  • Save sa-mm/539362ff431543b7639be17b9e181627 to your computer and use it in GitHub Desktop.
Save sa-mm/539362ff431543b7639be17b9e181627 to your computer and use it in GitHub Desktop.
Blocks push to master unless `-n` flag is provided
#!/bin/bash
# Stops accidental pushes to master. Adapted from https://gist.github.com/ColCh/9d48693276aac50cac37a9fce23f9bda
# Install in project:
# install or symlink at .git/hooks/pre-push
# chmod +x .git/hooks/pre-push
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
echo -en "\033[31mPush 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