Skip to content

Instantly share code, notes, and snippets.

@Pupskuchen
Created October 13, 2017 21:55
Show Gist options
  • Save Pupskuchen/39efb673be4f67708415efcbe88d1994 to your computer and use it in GitHub Desktop.
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/
#!/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