Last Updated: March 02, 2016
·
1.03K
· wojtekkruszewsk

Make git ask for confirmation before pushing to production repo

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.

Git pre-push hook to the rescue:

#!/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

1 Response
Add your response

My Bash-foo is very limited so suggestions for improvements are very welcome: https://gist.github.com/WojtekKruszewski/a6f203bcfdca691ded96

over 1 year ago ·