Bird
Raised Fist0
Gitdevops~10 mins

Sharing hooks with the team (husky) in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Sharing hooks with the team (husky)
Add husky to project
↓
Create hook script
↓
Commit hook config to repo
↓
Team pulls repo
↓
Husky installs hooks locally
↓
Hooks run automatically on git actions
This flow shows how husky hooks are added, committed, and shared so the whole team runs the same git hooks automatically.
Execution Sample
Git
npm install husky --save-dev
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npm test"
git add package.json .husky/pre-commit
git commit -m "Add pre-commit hook"
This code installs husky, sets up a pre-commit hook to run tests, and commits the hook config to the repo.
Process Table
StepActionResultEffect on Team
1Run npm install husky --save-devHusky added to dev dependenciesTeam members get husky when they install dependencies
2Run npm set-script prepare "husky install"Prepare script added to package.jsonTeam npm install runs husky install automatically
3Run npx husky add .husky/pre-commit "npm test"Pre-commit hook script createdHook script ready to share
4git add package.json .husky/pre-commitHook script and package.json staged for commitReady to share hook with team
5git commit -m "Add pre-commit hook"Hook config committed to repoTeam members get hook on next pull
6Team pulls repo and runs npm installHusky installs hooks locallyHooks run automatically on git actions
7Team makes commitPre-commit hook runs npm testEnsures code quality before commit
8Condition: npm test fails?Commit blockedPrevents bad code from entering repo
9Condition: npm test passes?Commit allowedCode safely committed
💡 Execution stops after commit is allowed or blocked by pre-commit hook
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 6
husky_installedfalsetruetruetruetruetrue
hook_script_existsfalsefalsefalsetruetruetrue
hook_committedfalsefalsefalsefalsetruetrue
team_hooks_installedfalsefalsefalsefalsefalsetrue
Key Moments - 3 Insights
Why do we commit the .husky folder to the repo?
Committing the .husky folder shares the hook scripts with the whole team, so everyone runs the same hooks automatically (see execution_table step 5).
What happens if a team member forgets to run npm install after pulling?
Husky hooks won't be installed locally, so git hooks won't run until they run npm install (see variable_tracker team_hooks_installed after step 6).
Does husky run hooks on the server or locally?
Husky runs hooks locally on each developer's machine before git actions like commit (see execution_table step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the pre-commit hook script created?
AStep 5
BStep 2
CStep 3
DStep 6
💡 Hint
Check the 'Action' column for when the hook script is created.
According to variable_tracker, when does the team_hooks_installed variable become true?
AAfter Step 3
BAfter Step 6
CAfter Step 5
DAfter Step 1
💡 Hint
Look at the 'team_hooks_installed' row and see when it changes to true.
If the npm test fails during the pre-commit hook, what happens according to the execution_table?
ACommit is blocked
BHook script is deleted
CCommit is allowed
DHusky uninstalls
💡 Hint
Check the rows describing the condition when npm test fails.
Concept Snapshot
Husky shares git hooks by committing hook scripts in .husky folder.
Install husky with npm and run 'husky install' to set up hooks.
Add hooks with 'husky add' and commit them.
Team members get hooks by pulling and running npm install.
Hooks run locally on git actions like commit to enforce rules.
Full Transcript
This lesson shows how to share git hooks using husky. First, install husky as a dev dependency. Then run 'npx husky install' to create the hooks folder. Add a hook script with 'npx husky add .husky/pre-commit "npm test"'. Commit the .husky folder so the hook script is shared. When team members pull and run npm install, husky installs the hooks locally. Now, when they commit, the pre-commit hook runs tests automatically. If tests fail, the commit is blocked, ensuring code quality. This process helps teams enforce rules consistently across all developers.

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

  1. Step 1: Understand what Git hooks do

    Git hooks are scripts that run at certain points in Git workflows, like before commits.
  2. Step 2: Identify Husky's role

    Husky helps share these hooks easily among team members by managing them in the project.
  3. Final Answer:

    To share Git hooks easily with the whole team -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify the command that sets up hooks

    The command husky install creates the hooks folder and configures Git to use Husky hooks.
  3. Final Answer:

    husky install -> Option C
  4. 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?
npm install husky
npx husky install
npx husky add .husky/pre-commit "npm test"
git commit -m "test commit"
medium
A. The commit will fail because the hook is not installed
B. The pre-commit hook runs 'npm test' before the commit
C. The commit message will be ignored
D. The pre-commit hook runs 'git status' before the commit

Solution

  1. Step 1: Analyze the commands

    First, Husky is installed. Then hooks are installed with husky install. Next, a pre-commit hook is added to run npm test.
  2. Step 2: Understand the effect on commit

    When git commit runs, the pre-commit hook triggers npm test before allowing the commit.
  3. Final Answer:

    The pre-commit hook runs 'npm test' before the commit -> Option B
  4. Quick Check:

    pre-commit runs npm test [OK]
Hint: Add hook with 'husky add' to run commands pre-commit [OK]
Common Mistakes:
  • Assuming hooks run after commit
  • Forgetting to run 'husky install'
  • Confusing hook commands with commit messages
4. You committed the .husky folder but your teammate's hooks are not running. What is the most likely cause?
medium
A. They forgot to run husky install after pulling
B. The .husky folder is ignored by Git
C. Hooks only work on Windows, not Linux
D. Git does not support hooks in shared repos

Solution

  1. Step 1: Understand sharing hooks with Husky

    Even if the .husky folder is committed, each user must run husky install locally to activate hooks.
  2. Step 2: Identify the common setup mistake

    If teammates don't run husky install, hooks won't be linked to Git and won't run.
  3. Final Answer:

    They forgot to run husky install after pulling -> Option A
  4. Quick Check:

    Run husky install locally to activate hooks [OK]
Hint: Run 'husky install' after pulling to activate hooks [OK]
Common Mistakes:
  • Assuming committed hooks auto-activate
  • Thinking hooks don't work on Linux
  • Believing Git disables hooks in shared repos
5. Your team wants to share a pre-push hook that runs tests before pushing. Which steps correctly set this up using Husky?
hard
A. Run git hook add pre-push npm test and push changes
B. Add pre-push: npm test in package.json, no other steps needed
C. Create a script named pre-push.sh and add it to .git/hooks manually
D. Run npx husky add .husky/pre-push "npm test", commit .husky, teammates run husky install

Solution

  1. Step 1: Add the pre-push hook with Husky

    Use npx husky add .husky/pre-push "npm test" to create the hook script that runs tests before push.
  2. Step 2: Share and activate hooks

    Commit the .husky folder so teammates get the hook, and they must run husky install locally to activate it.
  3. Final Answer:

    Run npx husky add .husky/pre-push "npm test", commit .husky, teammates run husky install -> Option D
  4. Quick Check:

    Add hook, commit folder, teammates install hooks [OK]
Hint: Add hook with husky add, commit folder, teammates run husky install [OK]
Common Mistakes:
  • Editing package.json instead of using husky add
  • Manually adding scripts to .git/hooks (not shared)
  • Using invalid git commands for hooks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard