Skip to content

Instantly share code, notes, and snippets.

@PaperNick
Created September 28, 2019 19:41
Show Gist options
  • Save PaperNick/08ba06b2e9e1f7d94150594d88689fff to your computer and use it in GitHub Desktop.
Save PaperNick/08ba06b2e9e1f7d94150594d88689fff to your computer and use it in GitHub Desktop.
Git commit hook - disallow commit in specific branches

How to install

#!/bin/bash

current_branch="$(git symbolic-ref --short HEAD)"

FORBIDDEN_BRANCHES=(master develop)
for forbidden_branch in "${FORBIDDEN_BRANCHES[@]}"; do
  if [ "$current_branch" == "$forbidden_branch" ]; then
    echo 'You are not allowed to commit in this branch.'
    exit 1
  fi
done
  1. Copy the content of this hook in /your/project/.git/hooks/pre-commit
  2. Make the pre-commit hook executable chmod +x /your/project/.git/hooks/pre-commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment