Last active
February 8, 2021 13:10
-
-
Save joaomarcos96/66fbc9a7705e248e9c00496e1b94eab9 to your computer and use it in GitHub Desktop.
git pre-push hook to confirm push force or delete on any branch
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
#!/bin/sh | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
push_command=$(ps -ocommand= -p $PPID) | |
is_destructive='force|delete|\-f' | |
confirm_destructive_action(){ | |
read -p 'Are you sure you want to push to "'$current_branch'" ? (y/n): ' -n 1 -r < /dev/tty | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
exit 0 | |
fi | |
echo 'Push aborted.' | |
exit 1 | |
} | |
if [[ $push_command =~ $is_destructive ]]; then | |
confirm_destructive_action | |
fi | |
unset confirm_destructive_action | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install pre-push hook:
<your_repo>/.git/hooks/pre-push
chmod +x <your_repo>/.git/hooks/pre-push