The Complete Production SOP: How to Deploy and Run Husky 9 & lint-staged Fast Git Pre-Commit Workflows in Autonomous AI Agents
Step-by-step SOP to install and run Husky 9 and lint-staged pre-commit hooks in Codex, Claude Code, and Cursor. Tested workflows, sandbox rules, and fixes.
Technical Overview & Architectural Context
Autonomous coding agents frequently generate multi-file edits, update imports, or refactor components without validating syntax, formatting, or styling rules before staging Git commits. Left unconstrained, agents often pollute version history with linting regressions, unformatted code, or broken commits that fail continuous integration pipelines. Historically, agents attempted to resolve this by executing broad repository-wide lint commands (`npm run lint`), which exhaust precious LLM context tokens, trigger hallucinated mass refactors across unrelated legacy code, and lead to execution timeouts. The Husky 9 & lint-staged skill enforces deterministic boundaries by scoping static analysis and auto-formatting strictly to staged Git files. Rather than relying on fuzzy prompts to remember formatting conventions, agents running this skill establish a zero-dependency shell pre-commit hook via Husky 9's streamlined architecture and configure lint-staged to run targeted formatters (such as ESLint and Prettier) exclusively on staged hunks. This ensures lightning-fast commit verification cycles and eliminates CI regressions before commits are finalized.
Prerequisites & Environment Requirements
Verified Search Intents & Long-Tail Queries Solved
This operational tutorial is engineered to provide deterministic solutions for these developer search queries:
Step-by-Step Execution Playbook
Local Environment Setup & SKILL.md Mounting
Create the local skill manifest directory and inject verified procedural rules into the agent context.
Agent runtimes such as Claude Code, Codex, and Cursor discover project-level skills by scanning standard `.agents/skills` or `.cursor/rules` directories. Mounting the `husky-git-hooks-lint-staged` SKILL.md provides the agent with structured instructions on how Husky 9 initializes hooks without legacy v8 shell wrappers (`.husky/_/husky.sh`), ensuring it uses the modern, lightweight `npx husky init` pattern.
mkdir -p .agents/skills/husky-git-hooks-lint-staged && curl -fsSL https://askills.dev/api/skills/husky-git-hooks-lint-staged/raw -o .agents/skills/husky-git-hooks-lint-staged/SKILL.md---
name: "husky-git-hooks-lint-staged"
description: "Automate linter and formatter execution on staged Git files using modern Husky 9 and lint-staged."
metadata:
author: "typicode"
version: "1.0.0"
runtime_compatibility:
- "codex"
- "claude-code"
- "cursor"
- "windsurf"
triggers:
- "install husky 9"
- "configure lint-staged"
- "pre-commit hook husky"
permissions:
fs_read: true
fs_write: "workspace"
shell_exec: true
net_access: true
---
# Husky 9 & lint-staged Fast Git Pre-Commit Workflows
1. Initialize Husky 9: Run `npx husky init` to generate `.husky/pre-commit` and append `prepare: husky` to package.json.
2. Overwrite `.husky/pre-commit` with `npx lint-staged`.
3. Add configuration to `package.json` under `lint-staged` mapping target file extensions to linters/formatters.
4. Validate staged execution using `git add` and dry-run linting before committing.Place the SKILL.md in the root `.agents/skills` directory so both Cursor multi-agent modes and Claude Code CLI can parse it without duplicating configuration across vendor-specific rule files.
Husky 9 completely deprecates `.husky/husky.sh` and the old `husky install` command. Ensure the agent does not invoke legacy `npx husky install` or create an obsolete `_` directory.
Dependency Installation & package.json Hook Wiring
Install Husky 9 and lint-staged as development dependencies and register the automated prepare lifecycle script.
Husky 9 relies on Node's native `prepare` lifecycle hook to automatically set Git's `core.hooksPath` to `.husky` whenever contributors or CI runners execute `npm install`. The agent must install the latest dev dependencies and execute `husky init` to initialize the directory structure deterministically.
npm install --save-dev husky@latest lint-staged@latest && npx husky init{
"scripts": {
"prepare": "husky"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,css,scss}": [
"prettier --write"
]
}
}Using `lint-staged` in `package.json` eliminates the need for separate `.lintstagedrc` files, saving token retrieval context when the agent parses repo configuration.
In monorepos or Docker environments without Git installed, `npx husky init` will exit with status 1. Always verify `.git` exists before executing.
Configuring the Pre-Commit Executable Hook
Write the fast lint-staged delegator into `.husky/pre-commit` and ensure executable POSIX file permissions.
Modern Husky 9 hooks are plain POSIX shell scripts. The agent must replace the boilerplate `npm test` generated by default in `.husky/pre-commit` with `npx lint-staged`. Running entire test suites on every pre-commit hook drastically slows developer velocity and causes LLM timeout aborts during multi-step automated commits.
echo 'npx lint-staged' > .husky/pre-commit && chmod +x .husky/pre-commit#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh" 2>/dev/null || true
npx lint-stagedKeeping the pre-commit hook focused strictly on `npx lint-staged` prevents test runner timeouts and ensures commit transactions finish within 500ms.
On macOS and Linux platforms, Git ignores hooks if the POSIX executable bit (`chmod +x`) is missing, resulting in silent commit passes without running linters.
Verification, Linting & Output Quality Gate
Execute an automated staging test to confirm that lint-staged intercepts dirty code and applies formatting.
The agent must create an intentionally unformatted temporary file, stage it with Git, and run `npx lint-staged --dry-run` or execute a trial commit. This guarantees that Git hooks execute as expected within the local agent workspace sandbox.
echo "const unformatted = 'bad spacing' ;" > src/test-husky-lint.ts && git add src/test-husky-lint.ts && npx lint-staged && git status --porcelain# Test that lint-staged runs without triggering an actual commit
git add src/test-husky-lint.ts
npx lint-staged --dry-run
# Cleanup temporary test artifact
git reset HEAD src/test-husky-lint.ts
rm -f src/test-husky-lint.tsUse `--dry-run` during agent automated testing to verify glob pattern matching without mutating files on disk or dirtying the working tree.
Never bypass failing pre-commit hooks using `git commit --no-verify` in autonomous agent scripts; this defeats the entire purpose of the quality gate.
Real-World Scenario: Setup Husky 9 and lint-staged on a TypeScript React Project
“Set up modern Husky 9 and lint-staged in this TypeScript repository. Ensure ESLint and Prettier run automatically on staged TypeScript and JSON files before any git commit is permitted.”
Runtime Troubleshooting & Failure Modes
Root Cause: The Git hook executed in a non-interactive shell where Node binaries or global path exports (such as nvm, fnm, or volta paths) were not inherited.
Prefix the command with npx in .husky/pre-commit: `echo 'npx --no -- lint-staged' > .husky/pre-commit`Root Cause: The `.husky/pre-commit` hook file was created without POSIX executable permissions, or Git's filemode config is disabled.
chmod +x .husky/pre-commit && git config core.fileMode trueFrequently Asked Questions (Husky 9 & lint-staged Fast Git Pre-Commit Workflows SOP)
Q:Can I use Husky 9 & lint-staged Fast Git Pre-Commit Workflows across Codex, Claude Code, and Cursor simultaneously?
Yes. This skill adheres to the open SKILL.md specification. Once installed under `.agents/skills/husky-git-hooks-lint-staged/SKILL.md` or as an IDE agent rule, Claude Code, Cursor, Codex, and Windsurf will uniformly read the operational SOP and execute identical CLI commands.
Q:How does Husky 9 & lint-staged Fast Git Pre-Commit Workflows prevent context window overflow during long agent sessions?
By constraining linting and formatting strictly to staged files via `lint-staged`, agents avoid reading hundreds of repository-wide linter errors into their active context window. This reduces prompt token consumption and eliminates hallucinated mass edits across untouched files.
Q:Does this skill require escalated root or container permissions?
No. The skill operates completely within standard workspace permissions (`fs_read: true`, `fs_write: workspace`, `shell_exec: true`). It modifies local project files and registers standard Git hooks without requiring root, sudo, or kernel-level access.
Ready to equip your autonomous agent with Husky 9 & lint-staged Fast Git Pre-Commit Workflows?
Copy the complete audited SKILL.md specification or download the 1-click zip bundle directly from our verified registry.