Course contentsShow
← Web Development
Lesson 1460 of 2,98226. Code Quality ToolsFree lesson

Pre-commit Hooks with Husky and lint-staged

Setting up Git hooks to automatically format staged files before commits for team-wide consistency.

Pre-commit Hooks with Husky and lint-staged

What you'll learn: How to automatically format and lint your code before every Git commit, ensuring your team never commits messy code.


The Core Idea

Imagine a security checkpoint at an airport—every passenger gets screened before boarding. Husky and lint-staged create a similar checkpoint for your Git commits. Before code reaches your repository, it automatically runs Prettier, ESLint, and Stylelint on only the files you're committing, fixing formatting issues and catching errors.

Without this automation, developers might forget to format code or skip linting. With pre-commit hooks, consistency happens automatically—no meetings, no code review comments about formatting, just clean code every time.


How It Works

Husky installs Git hooks in your project. A Git hook is a script that runs at specific moments (like before a commit). lint-staged is the smart assistant that says "only check the files being committed right now" instead of your entire codebase.

When you run git commit, Husky intercepts it, lint-staged identifies your staged files, runs Prettier to format them, runs ESLint to catch errors, and if everything passes, the commit proceeds. If linting fails, the commit is blocked until you fix the issues.


The Team Benefit

Once configured, every team member gets the same quality checks. New developers don't need to remember to run formatters—it happens automatically. Your CI/CD pipeline sees fewer formatting-related failures because bad code never makes it past the commit stage.

This is enforcing code quality before it enters version control, creating a consistent baseline across your entire team without manual effort.


Key Takeaway: Pre-commit hooks with Husky and lint-staged automate code formatting and linting at commit time, ensuring team-wide consistency without relying on developers to remember manual steps.