🚫💩 — Run tasks like formatters and linters against staged git files
🚫💩 — Run tasks like formatters and linters against staged git files
test
Run tasks like formatters and linters against staged git files and don't let :poop: slip into your code base!
npm install --save-dev lint-staged # requires further setup$ git commit
⋯ Backing up original state…
✔ Done backing up original state (1f4c047d)!
⋯ Running tasks for staged files…
*.{json,md} — 1 file
⋯ prettier --write
✔ prettier --write
✔ Done running tasks for staged files!
⋯ Staging changes from tasks…
✔ Done staging changes from tasks!
⋯ Cleaning up temporary files…
✔ Done cleaning up temporary files![!Tip] Do you only want to check staged files for errors, but not edit them automatically?
You might be interested in this simpler shell script:lint-staged.sh.
Code quality tasks like formatters and linters make more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a task on a whole project can be slow, and opinionated tasks such as linting can sometimes produce irrelevant results. Ultimately you only want to check files that will be committed.
This project contains a script that will run arbitrary shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
dotnet-format and lint-stagedTo install lint-staged in the recommended way, you need to:
npm install --save-dev lint-stagedpre-commit git hook to run lint-staged
{ "*.js": "eslint" } to run ESLint for all staged JS filesDon't forget to commit changes to package.json and .husky to share this setup with your team!
Now change a few files, git add or git add --patch some of them to your commit, and try to git commit them.
See examples and configuration for more information.
[!CAUTION]
Lint-staged runsgitoperations affecting the files in your repository. By default lint-staged creates agit stashas a backup of the original state before running any configured tasks to help prevent data loss.
See Releases.
For breaking changes, see MIGRATION.md.
…--allBy default lint-staged only runs tasks on files that include staged changes (hence the name). Use this flag to include all files tracked in Git version control (standard exclusions apply). Using this flag implies the --no-stash flag, disabling the automatic backup, and the --allow-empty flag so that lint-staged doesn't fail when there are no changes after running. This makes it easier to run npx lint-staged --all on a clean state, for example in CI.
--allow-emptyBy default, when tasks undo all staged changes, lint-staged will exit with an error and abort the commit. Use this flag to allow creating empty git commits.
--concurrent [number|boolean]Controls the concurrency of tasks being run by lint-staged. NOTE: This does NOT affect the concurrency of subtasks (they will always be run sequentially). Possible values are:
false: Run all tasks seriallytrue (default) : Infinite concurrency. Runs as many tasks in parallel as possible.{number}: Run the specified number of tasks in parallel, where 1 is equivalent to false.--config [path]Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and will print an error if the specified file cannot be found. If '-' is provided as the filename then the config will be read from stdin, allowing piping in the config like cat my-config.json | npx lint-staged --config -.
--cwd [path]Change the working directory lint-staged runs tasks in. Defaults to process.cwd() and the value can be absolute or relative to the default value.
--debugLog additional information about staged files, commands being executed, location of binaries, etc.
--diffBy default tasks are filtered against all files staged in git, generated from git diff --staged. This option allows you to override the --staged flag with arbitrary revisions. For example to get a list of changed files between two branches, use --diff="branch1...branch2". You can also read more from about git diff and gitrevisions. This option also implies --no-stash.
--diff-filter [string]By default only files that are added, copied, modified, or renamed are included. Use this flag to override the default ACMR value with something else: added (A), copied (C), deleted (D), modified (M), renamed (R), type changed (T), unmerged (U), unknown (X), or pairing broken (B). See also the git diff docs for --diff-filter.
--continue-on-errorBy default lint-staged will "exit early" when any of the configured tasks fails, to make sure the runtime is short. With this flag, lint-staged will instead run all tasks to completion and only fail at the end, allowing all task output to be seen.
--fail-on-changesBy default changes made by tasks are automatically staged and added to the commit. This flag disables the behavior and makes lint-staged exit with code 1, failing the commit instead. Using this flag also implies the --no-revert flag which means any changes made my tasks will be left in the working tree after failing, so that they can be manually staged and the commit tried again.
--max-arg-length [number]The list of staged files are appended to configured tasks as arguments, and the resulting string might be too long for the current shell, especially on Windows. Lint-staged tries to avoid this by splitting the list of staged files into chunks resulting in the commands being run multiple times. This behavior affects all tasks, including functions. Use the --max-arg-length to override the platform-specific default value if you want to avoid this. Setting --max-arg-length=Infinity will disable chunking completely.
--no-stashBy default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit.
--no-hide-partially-stagedBy default, unstaged changes from partially staged files will be hidden and applied back after running tasks. This option will disable this behavior, causing those changes to also be committed.
--hide-unstagedUse this option to hide all unstaged changes in tracked files, instead of just those which are also partially staged, before running tasks. The changes will be applied back after running the tasks.
--hide-allAdd new option --hide-all for hiding all unstaged changes and untracked files, before running tasks. This makes it easier to run tools like Knip which check for unused code. Untracked files are included in the backup stash and restored automatically after running.
--quietSuppress all CLI output, except from tasks.
--relativePass filepaths relative to process.cwd() (where lint-staged runs) to tasks. Default is false.
--no-revertBy default all task modifications will be reverted in case of an error. This option will disable the behavior, and apply task modifications to the index before aborting the commit.
--verboseShow task output even when tasks succeed. By default only failed output is shown.
Lint-staged can be configured in many ways:
lint-staged object in your package.json, or package.yaml.lintstagedrc file in JSON or YML format, or you can be explicit with the file extension:.lintstagedrc.json.lintstagedrc.yaml.lintstagedrc.yml.lintstagedrc.mjs or lint-staged.config.mjs file in ESM formatexport default { ... }.lintstagedrc.cjs or lint-staged.config.cjs file in CommonJS formatmodule.exports = { ... }lint-staged.config.js or .lintstagedrc.js in either ESM or CommonJS format, depending on
whether your project's package.json contains the "type": "module" option or not.--config or -c flagConfiguration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses picomatch for glob patterns. JavaScript files can also export advanced configuration as a function. See Using JS configuration files for more info.
You can also place multiple configuration files in different directories inside a project. For a given staged file, the closest configuration file will always be used and tasks will by default run in the directory of the config (for example, the directory of a specific package in a monorepo). See "How to use lint-staged in a multi-package monorepo?" for more info and an example.
package.json example:{
"lint-staged": {
"*": "your-cmd"
}
}.lintstagedrc.json example{
"*": "your-cmd"
}This config will execute your-cmd with the list of currently staged files passed as arguments.
So, considering you did git add file1.ext file2.ext, lint-staged will run the following command:
your-cmd file1.ext file2.ext
Lint-staged provides TypeScript types for the configuration and main Node.js API. You can use the defineConfig helper in your JS configuration files:
import { defineConfig } from 'lint-staged/config'
export default defineConfig({
'*': 'prettier --write',
})It's also possible to use
Allow function tasks to return parallel tasks
Backup stash is resolved by hash but dropped by index — a concurrent stash writer (e.g. a sibling git worktree) can have its entry destroyed
Supply `logger` or output array to function tasks
Feature: Allow passing arrays of globs
integration with NX affected