How can you create a custom git hook that prevents commit messages from exceeding 50 characters in a subject line?
So this is something that I came across recently and wanted to share with the community.
- Create a custom hook shell script
Navigate to the.git/hooks
directory and create a new file namedcommit-msg-50chars
cd <path-to-repo>/.git/hooks
touch commit-msg
chmod +x commit-msg
- Next, open the file and add the below script
#!/bin/sh
# git hook to check the length of the commit messages subject line
#reading the commit message
commit_msg_file=$1
commit_msg=$(head -n 1 "$commit_msg_file")
#checking the length
if [ ${#commit_msg} -gt 50 ]; then
echo "Error: the commit message exceeds 50 chars"
echo "max allowed chars - 50"
exit 1
fi