Skip to content

Instantly share code, notes, and snippets.

@wkrsz
Last active March 6, 2018 03:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkrsz/a6f203bcfdca691ded96 to your computer and use it in GitHub Desktop.
Save wkrsz/a6f203bcfdca691ded96 to your computer and use it in GitHub Desktop.
Git pre-push hook that shows changes and ask for confirmation when pushing to production repo.
Whenever you push to remote that has "production" in its name,
this hook will show commits and changes and then ask for confirmation.
Copy or symlink to .git/hooks/pre-push.
Deployment by pushing code to remote github repo are very convenient
but prone to accidents. I wanted something that would force me to
review what's being pushed.
My Bash-foo is very limited to suggestions for improvements are very welcome.
#!/bin/bash
if [[ "$1" =~ "production" ]]; then
read -a array
pushed_ref=${array[1]}
remote_ref=${array[3]}
echo
echo "# Commits:"
git log --oneline -5 $remote_ref...$pushed_ref
echo
echo "# Files changed:"
git diff --name-only $remote_ref $pushed_ref
echo
echo
read -p "You're about to push to production, are you sure? [AFFIRMATIVE|NEGATIVE] " -r < /dev/tty
echo
if [[ $REPLY = 'AFFIRMATIVE' ]]; then
exit 0
else
exit 1
fi
else
exit 0 # push will execute
fi
@Exadevelop
Copy link

Excellent!!! Work perfect.

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