Stack Builder

Husky

Configure Git hooks with ease.

Installation

Terminal
npm i -D husky

Create hook

husky init creates a pre-commit script in .husky/ and updates the prepare script in package.json.

Terminal
npx husky init
echo "echo 'Running pre-commit command'" > .husky/pre-commit

Test hook

Terminal
# Add the pre-commit file to staging
git add .husky/pre-commit
 
# Create a temporary commit, which should trigger the pre-commit hook
git commit -m "chore: Test pre-commit hook"
 
# The output should be "Running pre-commit command"
 
# Undo the last commit but keep the changes in the working directory
git reset --soft HEAD~1

If the hook works, comment out the echo command.

Terminal
echo "# echo 'Running pre-commit command'" > .husky/pre-commit

Last updated on

On this page

Edit on GitHub