Skip to content

Instantly share code, notes, and snippets.

@cpecorari
Created September 10, 2021 09:26
Show Gist options
  • Save cpecorari/fc2e97ccc909bc75e96fb9cfee4b01e2 to your computer and use it in GitHub Desktop.
Save cpecorari/fc2e97ccc909bc75e96fb9cfee4b01e2 to your computer and use it in GitHub Desktop.
pre-push protecting git branch
#!/bin/bash
protected_branch='prod'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push master, is that what you intended? [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
@cpecorari
Copy link
Author

  1. Create that script touch .git/hooks/pre-push
  2. Copy content
  3. Don't forget to set it to executable chmod +x .git/hooks/pre-push

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