Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Created April 14, 2017 15:25
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aaronhoffman/ffbfd36928f9336be2436cffe39feaec to your computer and use it in GitHub Desktop.
Save aaronhoffman/ffbfd36928f9336be2436cffe39feaec to your computer and use it in GitHub Desktop.
git hooks - prevent commit to local master branch and prevent push to remote master branch
#!/bin/sh
# prevent commit to local master branch
branch=`git symbolic-ref HEAD`
if [ "$branch" = "refs/heads/master" ]; then
echo "pre-commit hook: Can not commit to the local master branch."
exit 1
fi
exit 0
#!/bin/sh
# Prevent push to remote master branch
while read local_ref local_sha remote_ref remote_sha
do
if [ "$remote_ref" = "refs/heads/master" ]; then
echo "pre-push hook: Can not push to remote master branch."
exit 1
fi
done
exit 0
@aaronhoffman
Copy link
Author

aaronhoffman commented Apr 14, 2017

more info: http://aaron-hoffman.blogspot.com/2017/04/git-protect-local-master-branch-commit-push.html

Installation:
Place these files in your ~/.git/hooks directory.

@JiboStore
Copy link

what if you want to prevent git reset as well? so no forward, no backward too

@karfau
Copy link

karfau commented Nov 16, 2018

Thx for sharing. Luckily this gist spread across, since the link to the blogspot page doesn't show any related content.

@aaronhoffman
Copy link
Author

@karfau - this is likely an issue with ad blockers and blogspot unfortunately

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