Skip to content

Instantly share code, notes, and snippets.

@mdailey77
Created April 9, 2022 13:38
Show Gist options
  • Save mdailey77/fd6ba0121854c511395607ac902f330e to your computer and use it in GitHub Desktop.
Save mdailey77/fd6ba0121854c511395607ac902f330e to your computer and use it in GitHub Desktop.
Git pre-push hook to enforce branch naming conventions
#!/usr/bin/env bash
LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(task|master|develop|qa|tb|bug|story)[a-z0-9._-]{2,10}$"
message="This branch violates the branch naming rules. Please rename your branch."
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
echo "$message"
exit 1
fi
exit 0
@mdailey77
Copy link
Author

This file has to be placed in a .githooks directory inside your Git repository root folder. While in the repository root folder, run git config core.hooksPath .githooks to set the newly created .githooks folder as the default .githooks folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment