lint-staged

Running Only Changed Files with lint-staged

Linting the whole project on every commit gets slow, and blocks commits over files you did not touch. lint-staged 14,736 (github.com/lint-staged/lint-staged (https://github.com/lint-staged/lint-staged 14,736 ), MIT, Node.js 22.22.1 2,131 or later) runs each command only on the staged files matching a glob. Sam configures ESLint 39,810 and lint-staged, and checks the existing code first:

An ESLint configuration and a lint-staged rule for BookNestShell
cat > eslint.config.js <<'EOF'
// ESLint's flat config: the recommended rules, with Node.js globals
const js = require('@eslint/js');
const globals = require('globals');
module.exports = [
  js.configs.recommended,
  { languageOptions: { sourceType: 'commonjs', globals: globals.node } }
];
EOF
echo '{ "*.js": "eslint --fix --max-warnings=0" }' > .lintstagedrc.json
npx eslint . && echo "ESLint: no problems"
Output
ESLint: no problems

A clean baseline matters, or the first person to touch app.js inherits every old warning. --fix repairs what it can; --max-warnings=0 makes remaining warnings fail.

The hard part is staging, not linting. When a file is partially staged, lint-staged saves the whole state with git stash create and git stash store, hides the unstaged edits so tools see exactly what will be committed, runs the tasks, stages their fixes, and puts the unstaged edits back. If a task fails, it restores the saved state. The rules can also live under a "lint-staged" key in package.json.