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
The typical usage involves npm/yarn scripts or direct invocation:
npm install husky
npm set-script prepare "husky install"
npm run prepare
Then, to add hooks (example for pre-commit):
husky add .husky/pre-commit "npm test"
Or manually create and make executable hook files in the .husky/ directory:
~/.husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm test
PARAMETERS
install [directory]
Sets up husky by creating or updating the Git hooks directory (usually .git/hooks) to point to husky's scripts. An optional directory can specify where husky's configuration files (e.g., .husky/) are located. Typically run via a 'prepare' script in package.json during dependency installation.
uninstall
Removes husky's integration from the Git hooks directory, restoring the original state or removing symbolic links created by husky.
add
Adds a new hook script to the .husky/ directory.
set
Similar to 'add', but explicitly sets or replaces the content of a hook script with the given command(s). This is useful for directly defining or updating a hook's action.
run
Manually executes a specific husky hook script. This command is primarily used for testing or debugging hook logic outside of a live Git operation.
init
An internal command used for initializing husky's setup files and directory structure. It's typically part of husky's automated installation process and rarely invoked directly by users.
DESCRIPTION
husky is a popular and widely adopted tool that simplifies the management of Git hooks within development projects. It allows developers to define and run custom scripts automatically before or after specific Git actions, such as committing, pushing, or merging code. By integrating seamlessly with the project's version control system, husky helps enforce code quality standards, run linters, execute automated tests, or perform other necessary tasks. This ensures code consistency, prevents common issues, and maintains a high level of code integrity before changes are integrated into the main codebase. While extensively used in JavaScript/TypeScript ecosystems, its design is language-agnostic, relying on simple shell scripts for hook execution, making it versatile for any project.
CAVEATS
husky requires Node.js and a package manager (npm or yarn) to be installed on the system, as it's primarily used in Node.js-based projects. It also relies on Git being available. Users can bypass husky hooks during Git operations by explicitly using the --no-verify flag (e.g., git commit --no-verify). Hook scripts executed by husky run in the project's root directory, so paths within scripts should be relative to this location. Compatibility with certain CI/CD environments might require specific setup adjustments to ensure hooks run correctly.
CONFIGURATION
husky hooks are configured as simple shell scripts located in the .husky/ directory at the project's root. Each file name within this directory corresponds to a standard Git hook (e.g., .husky/pre-commit, .husky/pre-push). These files must be executable (e.g., chmod +x .husky/pre-commit) and contain the shell commands that husky will run when the corresponding Git action occurs. For example, a pre-commit file might contain commands like npm run lint or npm test.
TYPICAL WORKFLOW
The typical workflow for integrating husky into a project involves a few key steps:
1. Install husky as a development dependency: npm install --save-dev husky
2. Add a 'prepare' script to your package.json to automatically install husky's hooks: "prepare": "husky install"
3. Run the 'prepare' script (often automatically by npm install or by executing npm run prepare).
4. Create hook files directly in the .husky/ directory (e.g., echo 'npm test' > .husky/pre-commit and then chmod +x .husky/pre-commit) or use the husky add command to generate them.
HISTORY
husky emerged as a modern solution to simplify Git hook management, addressing the complexities of manually setting up and maintaining hook scripts across different development environments. Its popularity grew rapidly, especially within the JavaScript ecosystem, due to its ease of use and robustness. Earlier versions involved more direct modifications to the package.json file for hook configuration. However, versions 7 and above introduced a simpler, file-based configuration where hooks are plain executable shell scripts located in a dedicated .husky/ directory, making them easier to manage, track with version control, and less prone to configuration conflicts.