Git Commit Msg Hook - Validate Commit Messages with Real Examples
🔍 commit-msg Hook: What Does It Do?
The commit-msg hook is triggered after a commit message has been written, but before Git accepts it. It gives you a chance to validate the message content and either approve or reject the commit based on rules.
Location:
.git/hooks/commit-msg
Input: Path to the file containing the commit message (usually passed as$1)🔐 Purpose:
Decide “Is this commit message valid or not?”
🎯 Real-World Use Cases of commit-msg Hook
1. 🔠 Minimum Word Count Check
Prevent short or unclear messages. For example, block any message with fewer than 5 words:
#!/bin/bash
msg=$(cat "$1")
word_count=$(echo "$msg" | wc -w)
if [ "$word_count" -lt 5 ]; then
echo "❌ Commit message must be at least 5 words!"
exit 1
fi
2. ✅ Enforce Message Format (type: description)
Ensure every commit message follows a standard like feat:, fix:, or docs::
#!/bin/bash
msg=$(cat "$1")
if ! echo "$msg" | grep -qE "^(feat|fix|docs|chore|test|refactor): .+"; then
echo "❌ Commit message must start with type: description (e.g., feat: add login)"
exit 1
fi
3. 📛 Block Forbidden Words or Placeholders
Prevent accidental commits that contain placeholders like WIP, temp, or asdf:
#!/bin/bash
msg=$(cat "$1")
if echo "$msg" | grep -qiE "(WIP|asdf|temp)"; then
echo "❌ Commit message contains forbidden placeholder words."
exit 1
fi
4. 📏 Maximum Length Check
Limit the first line of the message to, say, 72 characters for Git log readability:
#!/bin/bash
msg=$(head -n1 "$1")
if [ "${#msg}" -gt 72 ]; then
echo "❌ Commit message should not exceed 72 characters in the first line."
exit 1
fi
5. 🧪 Run Tests Based on Message Type
Trigger automated tests when certain types of changes are committed:
#!/bin/bash
msg=$(cat "$1")
if echo "$msg" | grep -qE "^(test|fix):"; then
npm test || exit 1
fi
6. 🚫 Block Empty Commit Messages
Stop commits that use -m "" or leave the message blank:
#!/bin/bash
msg=$(cat "$1" | tr -d '\n')
if [ -z "$msg" ]; then
echo "❌ Empty commit message is not allowed!"
exit 1
fi
7. 🧾 Add Metadata to Message (Not Recommended)
⚠️ Note: Technically possible but discouraged. While you can mutate the commit message here, it’s safer to use the
prepare-commit-msghook for modifying content.
📦 Summary Table
| Use Case | Benefit |
|---|---|
| Min/Max Word Count | Enforce message clarity |
Format Enforcement (type: desc) | Team-wide consistency |
Block Placeholders (WIP) | Avoid accidental or temp commits |
| First-line Length Limit | Clean Git history |
| Run Tests Based on Message | Safe commits through automation |
| Block Empty Messages | Prevent meaningless commits |
🧪 Quick Setup for Testing
To try any of these examples:
touch .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
Paste your preferred script into the file above and try committing!
Final Thoughts
The commit-msg hook is a simple yet powerful way to improve your Git hygiene. From blocking incomplete commits to enforcing message standards, it ensures that your commit history remains clean, consistent, and professional.
Implement it today, and watch your commit quality go up instantly!
Let me know if you'd like this added to a Git automation series, need an accompanying YouTube script, or want a Hindi blog version as well.