0
0
Gitdevops~5 mins

Sharing hooks with the team (husky) in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Git hooks are scripts that run automatically at certain points in your git workflow. Husky helps you share these hooks easily with your whole team by storing them in the project, so everyone uses the same checks.
When you want to run tests automatically before anyone pushes code to the shared repository
When you want to enforce code style rules on every commit to keep the codebase consistent
When you want to prevent commits with sensitive data or mistakes from being added
When you want all team members to have the same git hooks without manual setup
When you want to automate tasks like linting or formatting before commits
Config File - package.json
package.json
{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "lint": "eslint .",
    "test": "jest"
  },
  "devDependencies": {
    "husky": "8.0.0",
    "eslint": "8.0.0",
    "jest": "29.0.0"
  }
}

This package.json file lists Husky and tools needed for linting and testing in devDependencies. Note that Husky v8+ uses a different setup method and does not configure hooks inside package.json. Instead, hooks are created as scripts inside the .husky folder using Husky commands.

Commands
Installs Husky as a development dependency so you can use git hooks in your project.
Terminal
npm install husky --save-dev
Expected OutputExpected
+ husky@8.0.0 added 1 package, and audited 2 packages in 2s 2 packages are looking for funding run `npm fund` for details found 0 vulnerabilities
Sets up Husky in the project by creating the hooks folder and enabling git hooks.
Terminal
npx husky install
Expected OutputExpected
husky - Git hooks installed
Adds a prepare script to package.json so Husky installs automatically when dependencies are installed.
Terminal
npm set-script prepare "husky install"
Expected OutputExpected
No output (command runs silently)
Creates a pre-commit hook script that runs linting before commits.
Terminal
npx husky add .husky/pre-commit "npm run lint"
Expected OutputExpected
husky - created .husky/pre-commit
Adds the Husky pre-commit hook script to git so it is shared with the team.
Terminal
git add .husky/pre-commit
Expected OutputExpected
No output (command runs silently)
Commits the Husky hook setup so everyone on the team gets the same hook when they pull.
Terminal
git commit -m "Add Husky pre-commit hook for linting"
Expected OutputExpected
[main abc1234] Add Husky pre-commit hook for linting 1 file changed, 10 insertions(+) create mode 100644 .husky/pre-commit
Key Concept

If you remember nothing else from this pattern, remember: Husky lets you store git hooks in your project files so the whole team uses the same automated checks.

Common Mistakes
Not running 'npx husky install' after installing Husky
Without this, Husky hooks are not set up and won't run on git actions.
Always run 'npx husky install' to initialize Husky hooks after installation.
Not committing the .husky folder to the repository
If the hooks folder is not committed, teammates won't get the hooks and checks won't run for them.
Add and commit the .husky folder and its scripts to share hooks with the team.
Forgetting to add a prepare script in package.json
Without the prepare script, Husky won't install hooks automatically when others run npm install.
Add 'prepare' script with 'husky install' in package.json to automate hook setup.
Summary
Install Husky as a dev dependency to manage git hooks.
Run 'npx husky install' to set up hooks in the project.
Add hook scripts using 'npx husky add' and commit the .husky folder to share hooks with the team.
Use package.json scripts to automate Husky installation on npm install.