Use a .git hooks script to prevent accidentally pushing to main branch
Pushing to main
branch when you meant to push to a new feature branch is embarrassing and annoying.
Github has a great per repo setting called “Branch protection rules” that can prevent your team from accidentally pushing to main
(or master
) branch. And also ensure that status checks like tests and linters all pass.
If you don’t have this set. Or if you’re an admin that’s allowed to override failed checks, nothing prevents you from pushing to main.
cat << 'DOC' > .git/hooks/pre-push
#!/bin/bash
protected_branch='main'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push to $protected_branch, 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
DOC
chmod +x .git/hooks/pre-push