From c29d444f83c781eeb4d64368e107a6858d35bfc8 Mon Sep 17 00:00:00 2001 From: i-afaqrashid <72761449+i-afaqrashid@users.noreply.github.com> Date: Wed, 22 Jul 2026 15:26:40 +0500 Subject: [PATCH] docs: add lefthook integration to pre-commit guide (#19191) Co-authored-by: fisker Cheung --- docs/precommit.md | 65 ++++++++++++++++++- .../version-stable/precommit.md | 65 ++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/docs/precommit.md b/docs/precommit.md index 02ec60830..2becf38e4 100644 --- a/docs/precommit.md +++ b/docs/precommit.md @@ -164,7 +164,70 @@ Add or remove file extensions to suit your project. Note that regardless of whic To read about how git-format-staged works see [Automatic Code Formatting for Partially-Staged Files](https://www.olioapps.com/blog/automatic-code-formatting/). -## Option 5. Shell script +## Option 5. [Lefthook](https://lefthook.dev/) + +**Use Case:** A fast, polyglot Git hooks manager with built-in parallelization. Useful if you want a single tool that can run Prettier alongside hooks for other languages in the same repo. + +Add a Prettier job to a `lefthook.yml` file at the repo root: + +```yaml title="lefthook.yml" +pre-commit: + jobs: + - name: prettier + run: npx prettier --ignore-unknown --write '{staged_files}' + stage_fixed: true +``` + +`stage_fixed: true` re-stages files that Prettier modified so the formatted version is what gets committed. + +Then install Lefthook and run `lefthook install` so Lefthook wires up the configured jobs into `.git/hooks`: + + + + +```bash +npm install --save-dev lefthook +npx lefthook install +``` + + + + +```bash +yarn add --dev lefthook +yarn lefthook install +``` + + + + +```bash +pnpm add --save-dev lefthook +pnpm lefthook install +``` + + + + +```bash +bun add --dev lefthook +bunx lefthook install +``` + + + + +```bash +deno install --dev lefthook +deno x lefthook install +``` + + + + +Re-run `lefthook install` whenever you change `lefthook.yml`. Read more at the [Lefthook](https://lefthook.dev/) website. + +## Option 6. Shell script Alternately you can save this script as `.git/hooks/pre-commit` and give it execute permission: diff --git a/website/versioned_docs/version-stable/precommit.md b/website/versioned_docs/version-stable/precommit.md index 02ec60830..2becf38e4 100644 --- a/website/versioned_docs/version-stable/precommit.md +++ b/website/versioned_docs/version-stable/precommit.md @@ -164,7 +164,70 @@ Add or remove file extensions to suit your project. Note that regardless of whic To read about how git-format-staged works see [Automatic Code Formatting for Partially-Staged Files](https://www.olioapps.com/blog/automatic-code-formatting/). -## Option 5. Shell script +## Option 5. [Lefthook](https://lefthook.dev/) + +**Use Case:** A fast, polyglot Git hooks manager with built-in parallelization. Useful if you want a single tool that can run Prettier alongside hooks for other languages in the same repo. + +Add a Prettier job to a `lefthook.yml` file at the repo root: + +```yaml title="lefthook.yml" +pre-commit: + jobs: + - name: prettier + run: npx prettier --ignore-unknown --write '{staged_files}' + stage_fixed: true +``` + +`stage_fixed: true` re-stages files that Prettier modified so the formatted version is what gets committed. + +Then install Lefthook and run `lefthook install` so Lefthook wires up the configured jobs into `.git/hooks`: + + + + +```bash +npm install --save-dev lefthook +npx lefthook install +``` + + + + +```bash +yarn add --dev lefthook +yarn lefthook install +``` + + + + +```bash +pnpm add --save-dev lefthook +pnpm lefthook install +``` + + + + +```bash +bun add --dev lefthook +bunx lefthook install +``` + + + + +```bash +deno install --dev lefthook +deno x lefthook install +``` + + + + +Re-run `lefthook install` whenever you change `lefthook.yml`. Read more at the [Lefthook](https://lefthook.dev/) website. + +## Option 6. Shell script Alternately you can save this script as `.git/hooks/pre-commit` and give it execute permission: