#!/bin/sh # .git/hooks/commit-msg message_file=$1 # This is the path to COMMIT-EDITMSG pattern="^(feat|fix|docs|style|refactor|test|chore)((.+))?: .+"
In the world of Git, much of the spotlight falls on commands like commit , push , merge , and rebase . Developers boast about their aliases, their branching strategies, and their elegant use of interactive rebasing. Yet, nestled quietly in the .git folder of every repository lies a humble, often-overlooked file: COMMIT-EDITMSG . COMMIT-EDITMSG
Located in .git/hooks/commit-msg (or .git/hooks/commit-msg.sample to start), this script can read, validate, or even modify the COMMIT-EDITMSG file before the commit is finalized. You want every commit message to follow the Conventional Commits standard (e.g., feat: add login , fix: resolve null pointer ). Located in
git commit --no-verify -m "Hotfix for production" Warning: Use sparingly. This is a nuclear bypass for emergency situations. It's easy to confuse COMMIT-EDITMSG with other .git files: This is a nuclear bypass for emergency situations
Your commit-msg hook can read .git/COMMIT_EDITMSG and reject the commit if it doesn't match the regex: