Live stream set for 2025-07-01 at 14:00:00 Eastern
Ask questions in the live chat about any programming or lifestyle topic.
This livestream will be on YouTube or you can watch below.
🚀 How to Use the Git commit-msg
Hook with PHP Projects
If you’re new to Git and PHP development, learning to automate and enforce commit standards is a great skill. In this article, you’ll learn how to use a Git commit-msg
hook to validate your commit messages, making your Git history cleaner and more professional – especially useful when working on a team or contributing to open source.
💡 What is a commit-msg
Hook?
Git hooks are scripts that run at various points in the Git lifecycle. The commit-msg
hook is triggered after you enter a commit message, but before the commit is finalized.
With it, you can:
- Enforce a specific format for commit messages
- Block empty or meaningless commits
- Require ticket numbers or prefixes (e.g. “FEATURE:”, “BUGFIX:”)
🔬 Example: Require Commit Messages to Start with FEATURE:
, BUGFIX:
, or DOC:
Create a commit-msg
hook in your project:
touch .git/hooks/commit-msg chmod +x .git/hooks/commit-msg
Paste the following into .git/hooks/commit-msg
:
#!/bin/sh COMMIT_MSG_FILE=$1 COMMIT_MSG=$(head -n1 "$COMMIT_MSG_FILE") case "$COMMIT_MSG" in FEATURE:*|BUGFIX:*|DOC:*) echo "✅ Commit message format OK" ;; *) echo "❌ Commit message must start with 'FEATURE:', 'BUGFIX:', or 'DOC:'" exit 1 ;; esac
Now try committing:
git commit -m "Refactored login"
You’ll see:
❌ Commit message must start with 'FEATURE:', 'BUGFIX:', or 'DOC:'
Try again:
git commit -m "FEATURE: Refactored login"
Now it works ✅
📷 Screenshots

.git/hooks/commit-msg
File
🎥 Screencast: Watch It in Action
📚 Want to Learn More PHP?
This tutorial is just one piece of the puzzle. If you’re serious about becoming a professional PHP developer, check out my full course and book:
🎓 Course: Learning PHP
📖 Book: Learning PHP
You’ll go from beginner to confident developer with step-by-step tutorials, projects, and real-world examples.
👨🎓 Need Help? I Offer One-on-One Coaching & Consulting
If you’re stuck, want code reviewed, or need help setting up a real-world PHP project, I’m available for one-on-one online tutorials and consulting.
📩 Contact me here or schedule a session.
✅ Final Thoughts
Git hooks are a hidden gem in the developer workflow. Starting with commit-msg
is a smart way to build good habits and prevent bad commits. Give it a try, and level up your Git game – your teammates will thank you!