Last active
August 29, 2015 14:16
-
-
Save vanjacosic/f50eac38c717afb60080 to your computer and use it in GitHub Desktop.
git pre-commit hook that prevents user from comitting to master branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Pre-commit hook to prevent commits in the master branch ('prod' in our case) | |
# Create a filed named 'pre-commit' in your local repository in the '.git/hooks' folder | |
# and make sure it is executable by using `chmod +x .git/hooks/pre-commit` | |
from subprocess import check_output | |
FORBIDDEN_BRANCH = 'prod' | |
CURRENT_BRANCH = str( | |
check_output( | |
'git symbolic-ref --short HEAD', shell=True) | |
).strip() | |
# print "Forbidden branch is: " + FORBIDDEN_BRANCH + "." | |
# print "Current branch is: " + CURRENT_BRANCH + "." | |
if FORBIDDEN_BRANCH == CURRENT_BRANCH: | |
print 'HOLD UP!' | |
print '' | |
print 'You are trying to commit to *{0}*'.format(CURRENT_BRANCH) + ' branch!' | |
print 'Are you sure you want to do this?' | |
print 'If you are, you can force commiting by adding the --no-verify flag.' | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment