An example of how to use a pre-commit hook to prevent a local commit to the default branch.
Created
April 12, 2024 17:58
-
-
Save stand-sure/cee84b483011acdca6f7f0116866e252 to your computer and use it in GitHub Desktop.
Preventing commits to the default branch with pre-commit
This file contains hidden or 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
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 |
This file contains hidden or 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/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