Sometimes you need to find a specific git commit.
Maybe you deleted some code you shouldn’t have. Maybe a feature that used to work suddenly doesn’t.
Now you’re going through commits, trying to guess which one you need based on commit messages

Pain.
When no rules are enforced, things default to chaos.
If you want a clean commit history with clear messages, you need two things
A clear commit message format
A way to enforce it automatically
And it’s actually quite easy to set up with Git hooks and a linter.
Git Hooks
A Git hook is a script that runs whenever a certain git event happens.
What are git events?
A typical git workflow looks like this:

Add, commit, push are the events.
These are a few common hooks:

Pre-commit runs before commit, pre-push runs before push, etc.
There are 14 client hooks in total.
If you log the .git/hooks folder, you will see all of them:

To use a hook, create a script with the same name.

Inside, you can run any script you want:
#!/bin/sh
#commit-msg
echo "Before commit, check the message"Now every time you do a “git commit -m”, git will log "Before commit, check the message."
Btw, commit message runs here:

Linting commit message
What we want is simple: every commit message should follow the same format.

To do that, use a tool like commitlint.
Install the required packages.
# install required packages
pnpm add -D @commitlint/cli @commitlint/config-conventional
# use default config
echo "export default { extends: ['@commitlint/config-conventional'] };" > commitlint.config.jsThen put this script inside the commit-msg file:

If the message doesn’t match your format?
The commit fails.
⚫️ input: Fixed. Yay
❌ subject may not be empty [subject-empty]
❌ type may not be empty [type-empty]Now you know how Git hooks works and the use case.
Fee from Anime Coders
PS: Git hooks only work locally. That’s why it’s not the best tool for collaboration. To solve this problem, developers often use it through a tool like Husky.
