You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
create following file in your repository folder .git/hooks/pre-receive
#!/usr/bin/env bash# Pre-receive hook that will block commits with messges that do not follow regex rule
commit_msg_type_regex='feat|fix|refactor|style|test|docs|build'
commit_msg_scope_regex='.{1,20}'
commit_msg_description_regex='.{1,100}'
commit_msg_regex="^(${commit_msg_type_regex})(\(${commit_msg_scope_regex}\))?: (${commit_msg_description_regex})\$"
merge_msg_regex="^Merge branch '.+'\$"
zero_commit="0000000000000000000000000000000000000000"# Do not traverse over commits that are already in the repository
excludeExisting="--not --all"
error=""whileread oldrev newrev refname;do# branch or tag get deletedif [ "$newrev"="$zero_commit" ];thencontinuefi# Check for new branch or tagif [ "$oldrev"="$zero_commit" ];then
rev_span=`git rev-list $newrev$excludeExisting`else
rev_span=`git rev-list $oldrev..$newrev$excludeExisting`fiforcommitin$rev_span;do
commit_msg_header=$(git show -s --format=%s $commit)if! [[ "$commit_msg_header"=~ (${commit_msg_regex})|(${merge_msg_regex}) ]];thenecho"$commit">&2echo"ERROR: Invalid commit message format">&2echo"$commit_msg_header">&2
error="true"fidonedoneif [ -n"$error" ];thenexit 1
fi
⚠ make .git/hooks/pre-receive executable (unix: chmod +x '.git/hooks/pre-receive')
Conventional Commit Messages
See how a minor change to your commit message style can make a difference.
Tip
Have a look at git-conventional-commits , a CLI util to ensure these conventions, determine version and generate changelogs
Commit Message Formats
Default
Merge Commit
Follows default git merge message
Revert Commit
Follows default git revert message
Inital Commit
Types
featCommits, that adds or remove a new featurefixCommits, that fixes a bugrefactorCommits, that rewrite/restructure your code, however does not change any API behaviourperfCommits are specialrefactorcommits, that improve performancestyleCommits, that do not affect the meaning (white-space, formatting, missing semi-colons, etc)testCommits, that add missing tests or correcting existing testsdocsCommits, that affect documentation onlybuildCommits, that affect build components like build tool, ci pipeline, dependencies, project version, ...opsCommits, that affect operational components like infrastructure, deployment, backup, recovery, ...choreMiscellaneous commits e.g. modifying.gitignoreScopes
The
scopeprovides additional contextual information.Breaking Changes Indicator
Breaking changes should be indicated by an
!before the:in the subject line e.g.feat(api)!: remove status endpointDescription
The
descriptioncontains a concise description of the change.This commit will...orThis commit should....) at the endBody
The
bodyshould include the motivation for the change and contrast this with previous behavior.Footer
The
footershould contain any information about Breaking Changes and is also the place to reference Issues that this commit refers to.BREAKING CHANGES:followed by space or two newlines. The rest of the commit message is then used for this.Examples
Git Hook Scripts to ensure commit message header format
Click to expand
commit-msg Hook (local)
pre-receive Hook (server side)
.git/hooks/pre-receive.git/hooks/pre-receiveexecutable (unix:chmod +x '.git/hooks/pre-receive')References