Husky Git Hooks Manager
Husky is a powerful tool that simplifies the management and execution of Git hooks in your projects. By integrating Husky, you can automate tasks before or after Git operations, ensuring code quality, consistency, and adherence to project standards.
Initialize Husky in Your Project
To start using Husky, you need to initialize it within your project. This typically involves running a command that sets up the necessary configuration files and installs Husky as a development dependency.
# Initialize husky for a project
npx husky-init && npm install
Adding and Managing Git Hooks
Once Husky is initialized, you can easily add new Git hooks. Hooks are scripts that run automatically at specific points in the Git workflow, such as before a commit or before a push. You can define custom commands to run for each hook.
# Add a new hook (e.g., pre-commit hook to run tests)
npx husky add .husky/pre-commit "npm test"
Enabling and Disabling Git Hooks
Husky manages your Git hooks by setting the
core.hooksPath
configuration in your Git repository. This
ensures that Git knows where to find your hook scripts.
# Enable Git hooks managed by Husky
git config core.hooksPath .husky
Uninstalling Husky Hooks
If you decide to remove Husky from your project, you can use the uninstall command to clean up the configuration and remove the hooks.
# Uninstall hooks created by Husky
npx husky uninstall
Upgrading Husky
As Husky evolves, new versions may introduce changes or improvements. It's recommended to upgrade your Husky configuration to the latest compatible version to benefit from these updates.
# Upgrade husky configuration from v4 to v7+
npx husky-upgrade
Removing Specific Hooks
You can also remove individual hooks manually if needed, by deleting
the corresponding script file within the
.husky
directory.
# Remove a specific hook (e.g., pre-commit)
rm .husky/pre-commit
Testing Git Hooks
For debugging or verification purposes, you can manually run a specific hook script to see its output and ensure it functions as expected.
# Run a specific hook manually for testing
npm run .husky/pre-commit