📖 Scenario: You are working on a team project using Git. Your team wants to ensure that everyone runs the same checks before committing code. To do this, you will use husky, a tool that helps share Git hooks easily across the team.
🎯 Goal: Set up husky in a project to share a pre-commit hook that runs npm test before every commit. This ensures all team members run tests before committing code.
📋 What You'll Learn
Create a package.json file with basic project info
Add a prepare script to install husky hooks
Initialize husky with npx husky install
Add a pre-commit hook that runs npm test
Verify the pre-commit hook runs on commit
💡 Why This Matters
🌍 Real World
Teams use husky to share Git hooks so everyone runs the same checks before committing code. This prevents errors and keeps code quality high.
💼 Career
Knowing how to set up and share Git hooks with husky is a valuable skill for developers and DevOps engineers to enforce team coding standards.
Progress0 / 4 steps
1
Create package.json with prepare script
Create a package.json file with a scripts section that includes a prepare script set to husky install.
Git
Hint
The prepare script runs automatically after npm install. It should run husky install to set up Git hooks.
2
Initialize husky hooks
Run the command npx husky install in the terminal to create the .husky folder and initialize husky hooks.
Git
Hint
This command creates a .husky folder in your project root. It is required to add Git hooks.
3
Add a pre-commit hook to run npm test
Add a pre-commit hook by running npx husky add .husky/pre-commit "npm test" in the terminal. This hook will run tests before every commit.
Git
Hint
This command creates a file .husky/pre-commit that runs npm test before commits.
4
Verify the pre-commit hook runs on commit
Run git commit -m "test commit" in the terminal. The pre-commit hook should run npm test before the commit completes.
Git
Hint
If the pre-commit hook is set up correctly, running git commit will trigger npm test before the commit finishes.
Practice
(1/5)
1. What is the main purpose of using Husky in a Git project?
easy
A. To share Git hooks easily with the whole team
B. To create new Git branches automatically
C. To merge pull requests without conflicts
D. To backup the Git repository to the cloud
Solution
Step 1: Understand what Git hooks do
Git hooks are scripts that run at certain points in Git workflows, like before commits.
Step 2: Identify Husky's role
Husky helps share these hooks easily among team members by managing them in the project.
Final Answer:
To share Git hooks easily with the whole team -> Option A
Quick Check:
Husky = Share Git hooks [OK]
Hint: Husky = sharing Git hooks with team [OK]
Common Mistakes:
Thinking Husky manages branches
Confusing Husky with backup tools
Assuming Husky merges pull requests
2. Which command correctly installs Husky hooks in your Git project?
easy
A. npm install husky
B. git husky add
C. husky install
D. husky init
Solution
Step 1: Recall Husky installation steps
First, you install Husky as a package (e.g., npm install husky), then you run husky install to set up Git hooks.
Step 2: Identify the command that sets up hooks
The command husky install creates the hooks folder and configures Git to use Husky hooks.
Final Answer:
husky install -> Option C
Quick Check:
Setup hooks = husky install [OK]
Hint: Use 'husky install' to set up hooks folder [OK]
Common Mistakes:
Using 'npm install husky' as hook setup
Trying 'git husky add' which is invalid
Confusing 'husky init' with 'husky install'
3. Given the commands below, what will be the result?