Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git hook to enforce branch names
#!/usr/bin/env bash
local_branch="$(git rev-parse --abbrev-ref HEAD)"
# valid_branch_regex="^(feature|bugfix|improvement|library|prerelease|release|hotfix)\/[a-z0-9._-]+$"
valid_branch_regex="^#?[0-9]{6,10}-[a-zA-Z]+$"
error()
{
echo "GIT PRE-COMMIT HOOK ERROR:"
echo "$1"
echo ""
echo "Your commit will be rejected."
echo "You should rename your branch to a valid name and try again."
exit 1
}
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
error "Branch names must match: $valid_branch_regex."
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment