Created
October 13, 2017 21:55
-
-
Save Pupskuchen/39efb673be4f67708415efcbe88d1994 to your computer and use it in GitHub Desktop.
pre-commit git hook to prevent unwanted direct commits to some branches – to be placed (and chmod +x'ed) in .git/hooks/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
restricted_branches=("develop") | |
forbidden_branches=("master") | |
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | |
if [[ " ${forbidden_branches[@]} " =~ " ${current_branch} " ]]; then | |
echo "Direct commits to ${current_branch} are not allowed." | |
exit 1 | |
fi | |
if [[ " ${restricted_branches[@]} " =~ " ${current_branch} " ]]; then | |
read -p "You're about to push to ${current_branch}. Are you sure this is what you want to do? [y|N]" -n 1 -r < /dev/tty | |
echo | |
if echo $REPLY | grep -E '^[Yy]$' > /dev/null | |
then | |
exit 0 | |
fi | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment