Skip to content

Instantly share code, notes, and snippets.

@stand-sure
Created April 12, 2024 17:58
Show Gist options
  • Save stand-sure/cee84b483011acdca6f7f0116866e252 to your computer and use it in GitHub Desktop.
Save stand-sure/cee84b483011acdca6f7f0116866e252 to your computer and use it in GitHub Desktop.
Preventing commits to the default branch with pre-commit
repos:
- repo: local
hooks:
- id: prevent-commits-to-default-branch
name: prevent commits to default branch
entry: prevent-commits-to-default-branch.sh
language: script
require_serial: true

An example of how to use a pre-commit hook to prevent a local commit to the default branch.

#!/usr/bin/env bash
DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$DEFAULT_BRANCH" == "$CURRENT_BRANCH" ]]; then
echo "you cannot commit to default branch: $DEFAULT_BRANCH"
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment