Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mozillazg
Created May 1, 2016 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mozillazg/b26a74f716ed5e786fe186fe64b8360c to your computer and use it in GitHub Desktop.
Save mozillazg/b26a74f716ed5e786fe186fe64b8360c to your computer and use it in GitHub Desktop.
protect master branch
protect_branch() {
protected_name=$1
current_name=$(git symbolic-ref HEAD | sed -e "s/^refs\/heads\///")
if [ "$current_name" = "$protected_name" ]; then
echo "!!! OH NO! you are trying to change the **$protected_name** branch!!!"
exit 1
fi
}
protect_branch master
@mozillazg
Copy link
Author

$ cat pre-commit >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
cat pre-commit >> .git/hooks/pre-push
chmod +x .git/hooks/pre-push

$ git branch
* master
  test
$ git commit -am "test"
!!! OH NO! you are trying to change the **master** branch !!!
$ git checkout test
A   a.tste
Switched to branch 'test'
$ git commit -am "test"
[test 6201659] test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.tste

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