Skip to content

Instantly share code, notes, and snippets.

@vanjacosic
Last active August 29, 2015 14:16
Show Gist options
  • Save vanjacosic/f50eac38c717afb60080 to your computer and use it in GitHub Desktop.
Save vanjacosic/f50eac38c717afb60080 to your computer and use it in GitHub Desktop.
git pre-commit hook that prevents user from comitting to master branch
#!/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