Course contentsShow
← JavaScript
Lesson 2598 of 2,72352. Linting and FormattingFree lesson

Git Hooks and Husky

Install Husky to manage Git hooks and trigger lint-staged automatically on pre-commit.

Git Hooks and Husky

You'll learn how to automatically enforce code quality by installing Husky to manage Git hooks that trigger lint-staged before every commit.

What Are Git Hooks?

Git hooks are scripts that run automatically at specific points in your Git workflow—like before you commit, after you push, or before you merge. Think of them as automatic quality gates that won't let bad code slip through. The most common hook is pre-commit, which runs right before Git finalizes your commit.

The problem? Native Git hooks live in .git/hooks/ and aren't tracked by version control, making them hard to share with your team.

Enter Husky

Husky is a tool that makes Git hooks easy to install, configure, and share across your entire team. It stores hook scripts in your repository (usually in .husky/), so everyone gets the same enforcement automatically.

When combined with lint-staged (which you've already configured), Husky ensures that ESLint and Prettier run on every commit—catching issues before they reach your codebase.

Installing Husky

npm install --save-dev husky
npx husky init

This creates a .husky/ folder in your project with a pre-commit hook file. Edit .husky/pre-commit to run lint-staged:

npx lint-staged

Now, every time you git commit, Husky triggers the pre-commit hook, which runs lint-staged, which in turn runs ESLint and Prettier on your staged files. If any issues are found that can't be auto-fixed, the commit is blocked.

Why This Matters

Without Husky, developers might forget to run linters manually. With it, quality checks become automatic and unavoidable—your codebase stays clean without relying on human memory.

Key Takeaway: Husky automates Git hooks to trigger lint-staged before commits, creating an enforceable quality gate that prevents poorly formatted or error-prone code from entering your repository.