Skip to content

Instantly share code, notes, and snippets.

@craig-davis
Forked from mbbx6spp/README.md
Last active September 8, 2015 22:21
Show Gist options
  • Save craig-davis/2c72ed89361a0c39e610 to your computer and use it in GitHub Desktop.
Save craig-davis/2c72ed89361a0c39e610 to your computer and use it in GitHub Desktop.
Git hooks to enforce pull request on master workflow

Sanity checking Git Hook for pre-commit

Checks that you are trying to push to master (or other key branches) from your local repository.

Installation

Download the above raw file (edit it as you please) and place inside your Git repository under: $GITDIR/hooks/pre-commit where $GITDIR is typically .git under your project working directory. Then make sure you make it executable: chmod +x .git/hooks/pre-commit

#!/usr/bin/env ruby
$restricted_branches = ['master', 'staging', 'qa', 'production']
# Prevents local committing to master branch.
# Will no effect pull requests on GitHub to the restricted branches,
# nor will it stop receives from remotes.
def verify_branch_name
branch_name = %x[git name-rev --name-only HEAD].chomp
if $restricted_branches.include?(branch_name)
abort('ERROR: Cannot commit to the #{branch_name} branch')
end
end
verify_branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment