LinuxCommandLibrary

husky

Manage Git hooks easily

TLDR

Install Husky in the current directory

$ husky install
copy

Install Husky into a specific directory
$ husky install [path/to/directory]
copy

Set a specific command as a pre-push hook for Git
$ husky set [.husky/pre-push] "[command] [command_arguments]"
copy

Add a specific command to the current pre-commit hook
$ husky add [.husky/pre-commit] "[command] [command_arguments]"
copy

Uninstall Husky hooks from the current directory
$ husky uninstall
copy

Display help
$ husky
copy

SYNOPSIS

husky <command> [<options>] [<args>]
Common: husky install | add <hook> <cmd> | uninstall

PARAMETERS

install
    Set up Git hooks by creating .husky directory and configuring .git/config

uninstall
    Remove Git hooks configuration from .git/config and optionally delete .husky

add
    Add a new hook script, e.g., husky add .husky/pre-commit "npm test"

init
    Initialize Husky in a new project (legacy, redirects to install)

set
    Update an existing hook script (alias for add in some versions)

remove
    Remove a hook script

-h, --help
    Show help for command

--version, -v
    Output version information

DESCRIPTION

Husky simplifies managing Git hooks in npm packages. It automatically configures Git hooks to run scripts during events like commit, push, or pre-merge.

After installing as a dev dependency (npm install husky --save-dev), run husky install to create a .husky directory with hook files. Edit these shell scripts to add commands, e.g., linting or testing before commits.

Husky supports multiple Node.js versions and works across platforms, including Linux. It uses a post-install script for setup and integrates seamlessly with package.json scripts. Key features include easy hook addition/removal, skipping hooks with HUSKY=0 git commit, and support for monorepos.

Ideal for teams enforcing code quality, Husky prevents bad commits by running checks automatically. It's widely used in JavaScript ecosystems but requires Node.js environment.

CAVEATS

Requires Node.js/npm; not a native Linux binary. Hooks skipped via HUSKY=0 env var. Version 5+ uses .husky dir; older versions used package.json config.

QUICK START

npm install husky --save-dev
husky install
husky add .husky/pre-commit "npm test"

SKIPPING HOOKS

Set env var HUSKY=0 git commit -m 'skip checks' to bypass all hooks.

HISTORY

Created by Tyler Kellen in 2013 as a simple Git hook tool. Evolved through versions: v4 used lint-staged integration, v5+ shifted to .husky folder for simplicity. Now at v9 (2024), maintained by Aristóteles Lima, with 20k+ GitHub stars and millions of weekly downloads.

SEE ALSO

git(1), npm(1), npx(1)

Copied to clipboard