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: Spotle…
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.