# Setup Pre-Commit Hooks (setup-pre-commit)

> Sets up a Husky pre-commit hook in the current repo that runs Prettier via lint-staged, plus typecheck and tests.

- Category: [Code Standards](https://www.myskills.tools/en/categories/code-standards/)
- Source: Community · Matt Pocock
- Repository: https://github.com/mattpocock/skills/tree/main/skills/misc/setup-pre-commit (commit 3216582)
- SKILL.md: https://github.com/mattpocock/skills/blob/main/skills/misc/setup-pre-commit/SKILL.md
- Install command: `npx skills add mattpocock/skills --skill setup-pre-commit`
- License: MIT
- Compatible with: Claude Code, Codex, Copilot, Gemini CLI
- Permissions: read-repo ✓, write-files ✓, shell ✓, network ✓
- Status: Scan passed
- A rule scan cannot catch every risk. Read this skill's SKILL.md and check its permissions before installing.
- ⚠ Installs dev dependencies, adds a git pre-commit hook and commits the changes.

## Core rule

Adapt the hook to the detected package manager, and omit the typecheck or test line, telling the user, when the repo has no such script.

## Execution Phases

1. **Detect and install** — Detect npm, pnpm, yarn or bun from the lockfile and install husky, lint-staged and prettier as devDependencies.
2. **Initialise Husky** — Run npx husky init, then write .husky/pre-commit to run lint-staged, typecheck and test.
3. **Add configs** — Create .lintstagedrc running prettier --ignore-unknown --write on staged files, and .prettierrc only if no Prettier config exists.
4. **Verify and commit** — Check the files and prepare script, run npx lint-staged, then commit so the new hook runs as a smoke test.

## Permissions (summarised from SKILL.md)

| Capability | Needed | Details |
|---|---|---|
| File writes | Limited | Creates or edits only .husky/pre-commit, .lintstagedrc, .prettierrc and package.json. |
| Command execution | Limited | Runs the package manager install, npx husky init, npx lint-staged and a git commit. |
| Network outbound | Limited | Only the package registry, to install dependencies. |

## SKILL.md frontmatter (YAML)

```yaml
---
name: setup-pre-commit
description: Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
---
```

Home: https://www.myskills.tools/en/ · URL: https://www.myskills.tools/en/skills/setup-pre-commit/
