husky
Manage Git hooks easily
TLDR
Install Husky in the current directory
Install Husky into a specific directory
Set a specific command as a pre-push hook for Git
Add a specific command to the current pre-commit hook
Uninstall Husky hooks from the current directory
Display help
SYNOPSIS
N/A (Husky is configured within package.json and operates automatically)
DESCRIPTION
Husky is a tool that simplifies the process of using Git hooks. It allows you to execute scripts when certain Git events occur, such as committing, pushing, or merging. This is useful for automating tasks like code linting, running tests, or enforcing commit message conventions before allowing the action to proceed.
Husky helps to configure git hooks by using your project's package.json file and the .git folder. It prevents committing or pushing bad code by running scripts automatically. By centralizing hook management in package.json, it improves collaboration and consistency across development teams, ensuring that all developers use the same hooks without manual setup. This approach can improve code quality, standardize commit messages, and prevent common errors.
CAVEATS
Husky requires Node.js and npm or yarn to be installed in the development environment. It modifies the .git/hooks directory, so ensure that you understand the implications for your Git repository.
CONFIGURATION
Husky is configured in your package.json file. You can specify the scripts to be executed for each Git hook. For example:{"husky": {"hooks": {"pre-commit": "lint-staged", "pre-push": "npm test"}}}
This configuration would run lint-staged before each commit and npm test before each push.
INSTALLATION
To install Husky, run: npm install husky --save-dev
or yarn add husky -D
. Then, enable git hooks using: npm set-script prepare "husky install"
. This ensures Husky is initialized when the project is installed.
You must run npm install
or yarn install
in order for prepare script to run.
HISTORY
Husky was created to simplify the setup and management of Git hooks, addressing the challenges of inconsistent hook configurations across different development environments. It has become a popular tool in the JavaScript ecosystem for enforcing code quality and standardization in Git workflows. Over time, Husky has evolved to support various package managers like npm and yarn, and its configuration options have become more flexible to accommodate different project needs.
SEE ALSO
git(1), pre-commit(1)