Skip to content

Instantly share code, notes, and snippets.

@vishalzambre
Created May 29, 2015 11:29
Show Gist options
  • Save vishalzambre/7f9ace75ed5a7b51a8cc to your computer and use it in GitHub Desktop.
Save vishalzambre/7f9ace75ed5a7b51a8cc to your computer and use it in GitHub Desktop.
Git: Prevent pushing to master
# For any repository you want to protect, you need to create a new file called pre-push in the .git/hooks directory.
# Also check that the pre-push file is executable (run chmod +x .git/hooks/pre-push on the command line) and try again :)
# Add following code snippets to $YOUR_REPO/.git/hooks/pre-push file
# Then run chmod +x $YOUR_REPO/.git/hooks/pre-push
#!/bin/bash
PROTECTED_BRANCH='master'
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
fi
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment