Skip to content

Instantly share code, notes, and snippets.

@ktajpuri
Created September 15, 2023 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktajpuri/990601361e778046f1b3a05aa7607e79 to your computer and use it in GitHub Desktop.
Save ktajpuri/990601361e778046f1b3a05aa7607e79 to your computer and use it in GitHub Desktop.
Add a pre commit hook for the format of git message
1. cat > .git/hooks/pre-commit
#!/bin/bash
# Extract the commit message from the last commit
commit_message=$(git log --format=%B -n 1 HEAD)
# Check if commit message contains a JIRA ID
if [[ ! $commit_message =~ [A-Z]+-[0-9]+ ]]; then
echo "Error: Commit message must include a JIRA ID."
exit 1
fi
# Check if commit message length is at least 30 characters
commit_length=$(echo -n "$commit_message" | wc -c)
if [[ $commit_length -lt 30 ]]; then
echo "Error: Commit message must be at least 30 characters long."
exit 1
fi
2. chmod +x .git/hooks/pre-commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment