Skip to content

Instantly share code, notes, and snippets.

@cy4n
Last active August 14, 2018 14:51
Show Gist options
  • Save cy4n/f53cb7ad593637b10f3c46cb14e73e9f to your computer and use it in GitHub Desktop.
Save cy4n/f53cb7ad593637b10f3c46cb14e73e9f to your computer and use it in GitHub Desktop.
pre-push hook to prevent from accidently pushing to master/develop/whateverbranch
#!/bin/bash
## add your branches here, like
## protected=('master' 'development' 'yourbranch')
protected=('master' 'develop')
current_branch=$(git rev-parse --abbrev-ref HEAD)
if (printf '%s\n' "${protected[@]}" | grep -xq $current_branch)
then
read -p "really push to $current_branch? [y|n] " -n 1 -r < /dev/tty
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
@cy4n
Copy link
Author

cy4n commented Aug 14, 2018

  1. copy to .git/hooks/pre-push
  2. chmod +x .git/hooks/pre-push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment