Bird
Raised Fist0
Gitdevops~20 mins

Sharing hooks with the team (husky) in Git - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Husky Hook Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of installing Husky with the correct command?
You run the command npm install husky --save-dev in your project. What is the expected output message you should see to confirm Husky installed successfully?
Ahusky is not recognized as an internal or external command
Berror: command not found: husky
Cnpm ERR! missing script: husky
Dadded 1 package, and audited 1 package in 2s
Attempts:
2 left
💡 Hint
Look for the message that confirms a package was added without errors.
🔀 Workflow
intermediate
1:30remaining
Which step correctly sets up Husky hooks to be shared with the team?
After installing Husky, which command should you run to initialize Husky hooks so they are tracked in your Git repository and shared with your team?
Anpx husky install
Bgit init
Cnpm start
Dhusky add .husky/pre-commit "npm test"
Attempts:
2 left
💡 Hint
This command creates the hooks folder and sets up Git hooks.
❓ Configuration
advanced
2:00remaining
Which package.json script ensures Husky hooks are installed automatically after cloning?
You want Husky hooks to be installed automatically when a team member clones the repo and runs npm install. Which package.json script configuration achieves this?
A"prepare": "husky install"
B"postinstall": "husky install"
C"preinstall": "husky install"
D"install": "husky install"
Attempts:
2 left
💡 Hint
This script runs automatically after dependencies are installed and before publishing.
❓ Troubleshoot
advanced
2:00remaining
Why does the Husky hook not run after cloning the repo?
A team member cloned the repo and ran npm install, but Husky hooks do not run on Git commits. What is the most likely cause?
AGit is not installed on the team member's machine
BThe .husky folder is ignored by .gitignore
CThe <code>prepare</code> script is missing in package.json, so hooks were not installed
DThe team member did not run <code>npx husky add</code> manually
Attempts:
2 left
💡 Hint
Check if the hooks installation step runs automatically after install.
✅ Best Practice
expert
2:30remaining
What is the best way to share a new pre-commit hook with your team using Husky?
You want to add a new pre-commit hook that runs npm test and share it with your team. Which sequence of commands and steps is best practice?
ACreate a shell script with tests and share it via email
BRun <code>npx husky add .husky/pre-commit "npm test"</code>, commit the .husky folder, and push to the repo
CRun <code>npx husky install</code> on your machine only, no commit needed
DAdd <code>npm test</code> to package.json scripts, then tell team to run it manually
Attempts:
2 left
💡 Hint
Think about how Git tracks files and how Husky hooks are stored.

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