Convert Figma logo to code with AI

typicode logohusky

Git hooks made easy 🐶 woof!

32,324
1,020
32,324
49

Top Related Projects

🚫💩 — Run linters on git staged files

16,798

The commitizen command line utility. #BlackLivesMatter

📓 Lint commit messages

A framework for managing and maintaining multi-language pre-commit hooks.

Fast and powerful Git hooks manager for any type of projects.

1,607

Codemods for your project config files

Quick Overview

Husky is a popular tool for managing Git hooks in JavaScript projects. It allows developers to easily add pre-commit, pre-push, and other Git hooks to their projects, enabling automated tasks like linting, testing, and code formatting before commits or pushes.

Pros

  • Easy to set up and configure
  • Integrates seamlessly with npm scripts
  • Supports all Git hooks
  • Improves code quality and consistency across team projects

Cons

  • Requires Node.js to be installed
  • Can slow down Git operations if hooks are resource-intensive
  • May require additional configuration for certain CI/CD environments

Code Examples

  1. Basic pre-commit hook:
{
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint && npm test"
    }
  }
}

This example runs linting and tests before each commit.

  1. Using lint-staged for optimized linting:
{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.js": ["eslint --fix", "prettier --write"]
  }
}

This example uses lint-staged to run ESLint and Prettier only on staged JavaScript files.

  1. Custom script execution:
{
  "husky": {
    "hooks": {
      "pre-push": "npm run build && npm run deploy"
    }
  }
}

This example runs a build and deploy script before pushing to the remote repository.

Getting Started

  1. Install Husky:
npm install husky --save-dev
  1. Add a prepare script to your package.json:
{
  "scripts": {
    "prepare": "husky install"
  }
}
  1. Add a hook:
npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit

This sets up a pre-commit hook that runs your tests before each commit.

Competitor Comparisons

🚫💩 — Run linters on git staged files

Pros of lint-staged

  • Focuses specifically on running linters and other checks on staged files
  • Optimizes performance by only running checks on changed files
  • Integrates seamlessly with various linting tools and formatters

Cons of lint-staged

  • Requires additional setup and configuration compared to Husky
  • Limited to pre-commit hooks, while Husky supports various Git hooks
  • May need extra plugins or scripts for more complex workflows

Code Comparison

lint-staged configuration:

{
  "lint-staged": {
    "*.js": "eslint --fix",
    "*.{js,css,md}": "prettier --write"
  }
}

Husky configuration:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

Summary

lint-staged excels at optimizing linting and formatting processes for staged files, making it ideal for projects focused on code quality checks before commits. It offers better performance for large codebases by targeting only changed files. However, it has a narrower scope compared to Husky, which provides a more comprehensive Git hooks solution. While lint-staged requires more initial setup, it integrates well with various tools and can be used in conjunction with Husky for a more robust pre-commit workflow.

16,798

The commitizen command line utility. #BlackLivesMatter

Pros of cz-cli

  • Provides a standardized commit message format
  • Offers interactive prompts for creating commit messages
  • Supports custom commit message templates and adapters

Cons of cz-cli

  • Requires additional setup and configuration
  • May be overkill for smaller projects or teams
  • Limited to commit message formatting, unlike Husky's broader scope

Code Comparison

cz-cli:

{
  "scripts": {
    "commit": "cz"
  },
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }
}

Husky:

{
  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}

Summary

cz-cli focuses on standardizing commit messages through interactive prompts and customizable templates. It's beneficial for teams wanting consistent, well-formatted commit messages but requires additional setup.

Husky, on the other hand, offers a broader range of Git hooks and is easier to set up for various pre-commit and commit-msg tasks. It's more versatile but doesn't provide the same level of commit message standardization as cz-cli without additional tools.

Choose cz-cli for detailed commit message formatting or Husky for a wider range of Git hook capabilities.

📓 Lint commit messages

Pros of commitlint

  • Focuses specifically on enforcing commit message conventions
  • Provides a wide range of built-in commit message rules
  • Easily integrates with continuous integration systems

Cons of commitlint

  • Limited to commit message linting, unlike Husky's broader Git hooks capabilities
  • Requires additional setup for Git hook integration
  • May have a steeper learning curve for teams new to conventional commits

Code Comparison

commitlint configuration:

{
  "extends": ["@commitlint/config-conventional"],
  "rules": {
    "type-enum": [2, "always", ["feat", "fix", "docs", "style", "refactor", "test", "chore"]]
  }
}

Husky configuration:

{
  "hooks": {
    "pre-commit": "npm test",
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
}

Summary

commitlint is a specialized tool for enforcing commit message conventions, offering built-in rules and easy CI integration. However, it has a narrower focus compared to Husky's broader Git hooks capabilities. Husky provides a more flexible approach to managing various Git hooks, while commitlint excels in standardizing commit messages. Teams should consider their specific needs and workflow when choosing between these tools, or potentially use them in combination for a comprehensive Git workflow management solution.

A framework for managing and maintaining multi-language pre-commit hooks.

Pros of pre-commit

  • Language-agnostic, supporting multiple programming languages and tools
  • Extensive library of pre-configured hooks available
  • Can be used locally and in CI/CD pipelines

Cons of pre-commit

  • Requires Python installation
  • More complex setup process
  • Less seamless integration with npm scripts

Code Comparison

pre-commit configuration (.pre-commit-config.yaml):

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer

husky configuration (package.json):

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

pre-commit offers a more flexible and extensible approach, supporting various languages and tools. It provides a wide range of pre-configured hooks and can be used in different environments. However, it requires Python and has a more complex setup process.

Husky, on the other hand, is specifically designed for JavaScript projects and integrates seamlessly with npm scripts. It's easier to set up and doesn't require additional language installations, but it's limited to JavaScript ecosystems and has fewer pre-configured hooks available.

Choose pre-commit for multi-language projects or when extensive customization is needed. Opt for Husky in JavaScript-centric projects for simpler setup and npm integration.

Fast and powerful Git hooks manager for any type of projects.

Pros of Lefthook

  • Language-agnostic, supporting multiple programming languages and environments
  • Faster execution with parallel running of hooks
  • More flexible configuration options, including YAML support

Cons of Lefthook

  • Requires separate installation and setup process
  • Less widespread adoption and community support
  • Steeper learning curve for configuration

Code Comparison

Husky configuration (package.json):

{
  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "pre-push": "npm run lint"
    }
  }
}

Lefthook configuration (lefthook.yml):

pre-commit:
  parallel: true
  commands:
    test:
      run: npm test
pre-push:
  commands:
    lint:
      run: npm run lint

Both Husky and Lefthook are Git hooks management tools, but they differ in their approach and features. Husky is more straightforward to set up and widely adopted, making it a popular choice for many projects. Lefthook offers more flexibility and performance benefits, especially for larger projects with complex hook requirements. The choice between the two depends on the specific needs of the project and the development team's preferences.

1,607

Codemods for your project config files

Pros of mrm

  • More versatile, handling various config files beyond Git hooks
  • Supports custom tasks and configurations
  • Can manage multiple projects with shared configurations

Cons of mrm

  • Steeper learning curve due to more complex functionality
  • Requires more setup and configuration than Husky
  • Less focused on Git hooks specifically

Code Comparison

Husky (in package.json):

{
  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "pre-push": "npm run lint"
    }
  }
}

mrm (in .mrm/config.json):

{
  "aliases": {
    "lint": "eslint . --fix",
    "test": "jest"
  },
  "config": {
    "lint": {
      "eslintrc": {
        "extends": ["eslint:recommended"]
      }
    }
  }
}

Both Husky and mrm are tools for managing project configurations, but they serve different purposes. Husky focuses specifically on Git hooks, making it easier to set up and use for that particular task. mrm, on the other hand, is a more comprehensive tool for managing various config files and project setups across multiple projects. While mrm offers more flexibility and power, it also requires more time to learn and set up compared to Husky's straightforward approach to Git hooks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

husky

Node.js CI

Modern native Git hooks made easy

Husky improves your commits and more 🐶 woof!

👋 Hey! Using React, Vue or Astro? Check my new project MistCSS to write 50% less code.

Features

  • Just 2 kB (📦 gzipped) with no dependencies
  • Extremely fast (runs in ~1ms)
  • Uses new Git feature (core.hooksPath)
  • Supports:

And more:

  • Branch-specific hooks
  • Use POSIX shell to script advanced cases
  • Adheres to Git's native hook organization
  • Aligns with npm best practices using prepare script
  • Opt-in/opt-out options
  • User-friendly error messages

Changelog

Check out the v9 changelog to discover all the new and improved features!

Documentation

https://typicode.github.io/husky

Important Upgrading from v4 to v9 requires migrating previous config, please see the docs.

Sponsors

Support this project by becoming a sponsor here 💖

Special Sponsor


Get rewards for your open-source contributions

GitHub

Open Collective

NPM DownloadsLast 30 Days