What is husky?
Husky is an npm package that allows you to manage Git hooks easily. It lets you run scripts at specific points in your Git workflow, such as before committing or pushing code. This helps in automating tasks like code linting, testing, and validation before these actions take place, ensuring code quality and consistency.
What are husky's main functionalities?
Pre-commit Hook
This feature allows you to run commands before a commit is made. In this example, `npm test` and `npm run lint` are executed before each commit, ensuring that tests pass and the code is linted.
"husky": {
"hooks": {
"pre-commit": "npm test && npm run lint"
}
}
Pre-push Hook
This feature allows you to run commands before code is pushed to the repository. In this example, `npm run build` is executed before each push, ensuring that the build is updated.
"husky": {
"hooks": {
"pre-push": "npm run build"
}
}
Commit-msg Hook
This feature allows you to run a script to validate the commit message. In this example, `validate-commit-msg.js` is a script that checks if the commit message follows a certain format.
"husky": {
"hooks": {
"commit-msg": "./validate-commit-msg.js"
}
}
Other packages similar to husky
lint-staged
Lint-staged is similar to Husky in that it is often used in conjunction with Husky to run linters on staged files in Git. It does not manage Git hooks itself but is designed to run scripts on files that are staged for commit.
pre-commit
Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. It is similar to Husky but is language-agnostic and can be used outside of the Node.js ecosystem. It requires a separate installation and configuration.
husky
Git hooks made easy
Husky can prevent bad commit, push and more :dog: woof!
Used by jQuery, Kibana, Paper.js, JSON Server, Hotel and many other awesome projects.
Usage
npm install husky --save-dev
{
"scripts": {
"precommit": "npm test",
"prepush": "npm test",
"...": "..."
}
}
git commit -m "Keep calm and commit"
Existing hooks aren't replaced and adding --no-verify
to your git commands lets you bypass hooks. You can also use any Git hook. Optionally include the environment variable $GIT_PARAMS
in your scripts to access any command-line parameters provided by git.
Tips
Debug
If you need to debug hooks, use npm run <script-name>
, for example:
npm run precommit
GUI applications
If you've installed Node using the standard installer, nvm or homebrew, git hooks will be executed even in GUI applications.
NVM
If you're using nvm, husky will try to use the default
installed version or use the project .nvmrc
file.
Uninstall
To uninstall husky and Git hooks, simply run:
npm uninstall husky --save-dev
License
MIT - typicode