Prevent git from pushing to the master branch

Here's how I prevent git from pushing to the master branch. I borrowed code from the blog post GIT Hack: Prevent pushing to master and simplified it.

Place the following code in a file called "pre-push" (with no file extension) in your git repo in the [repo]/.git/hooks folder so the actual path to the file is [repo]/.git/hooks/pre-push:

#!/bin/sh
 
## Protects master branch from being pushed.
 
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
 
if [ $protected_branch = $current_branch ]
then
  echo "Pushing to master is not allowed. See .git/hooks/pre-push"
  exit 1
fi

Internal References

External References

Article Type

General