I thought about writing this because pre-commit checks are one of the most overlooked parts of the development process. Many developers push code straight to GitHub or GitLab without realizing that a simple setup could save them countless hours of debugging and keep their projects clean and consistent.
So, let’s talk about pre-commit hooks and how Husky helps make them easy to use.
What Are Pre-Commit Hooks?
Pre-commit hooks are scripts that run automatically before a Git commit is finalized.
They allow you to run checks or commands that ensure your code meets certain standards before it’s saved into the repository.
Think of them as automated gatekeepers for your commits.
For example, a pre-commit hook can:
- Run your tests and stop the commit if something fails
- Lint and format your code automatically
- Enforce commit message standards
- Prevent large or sensitive files from being committed
The main goal is simple: catch issues early and maintain consistency across your team.
The Problem With Manual Git Hooks
Git actually supports hooks natively. You can find them in the .git/hooks folder of any repository.
However, these hooks aren’t shared when you clone or pull a repo because they’re stored locally.
That means every developer has to set them up manually — and that’s both inconvenient and error-prone.
This is where Husky comes in.
Enter Husky
Husky is a tool that makes it easy to manage and share Git hooks within your project.
Instead of manually editing .git/hooks, Husky lets you configure hooks right inside your repository so everyone benefits automatically.
With Husky, you can automate tasks like linting, formatting, or running tests before every commit or push — all version-controlled and consistent for every developer.
Setting Up Husky
Here’s how you can set it up quickly:
# Step 1: Install Husky
npm install husky --save-dev
# Step 2: Enable Git hooks
npx husky install
# Step 3: Add a pre-commit hook
npx husky add .husky/pre-commit "npm test"
Now, every time you commit, your tests will run automatically.
If they fail, the commit won’t go through, protecting your main branch from broken code.
You can also use tools like ESLint, Prettier, or lint-staged to format and lint your code before commits.
For example:
npx husky add .husky/pre-commit "npx lint-staged"
And in your package.json:
{
"lint-staged": {
"*.{js,ts,jsx,tsx}": ["eslint --fix", "prettier --write"]
}
}
This ensures only the staged files are linted and formatted, making the process faster.
Other Pre-Commit Tools You Should Know
Different languages and ecosystems have their own pre-commit tools. Here are a few popular ones:
- JavaScript/TypeScript: Husky, Lefthook, Yorkie
- Python: pre-commit
- Ruby: Overcommit
- Go: pre-commit-go
- Java: Spotless
- C#/.NET: GitHooks.NET
No matter what stack you’re working with, the idea remains the same: ensure quality before the code lands in your repo.
Why I Think This Matters
Developers often focus on writing features and fixing bugs, but forget to automate the boring parts that ensure quality.
Pre-commit hooks are one of those simple setups that can dramatically improve your workflow and team collaboration.
Husky, in particular, makes it effortless for JavaScript and TypeScript projects to keep their codebase clean, consistent, and ready for production, every single commit.
So if you’ve never used pre-commit hooks before, now’s a good time to start.
Top comments (0)