> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Git hooks

Use [`rs hooks`](/guide/cli/hooks.md) to manage repository-level Git hooks that run project commands. By default, hook scripts live in `.rstack/hooks`. You can use them to validate commit messages, check code before pushing, or format files before committing.

This page uses `pre-commit` as an example: first install Git hooks with `rs hooks`, then run [`rs staged`](/guide/cli/staged.md) from the `pre-commit` hook to lint and format the files staged for the commit.

## Set up hooks

Add `rs hooks` to the `prepare` script of the project that owns the repository hooks:

```json title="package.json"
{
  "scripts": {
    "prepare": "rs hooks"
  }
}
```

Run the script once to install the hooks:


```sh [npm]
npm run prepare
```

```sh [yarn]
yarn run prepare
```

```sh [pnpm]
pnpm run prepare
```

```sh [bun]
bun run prepare
```

`rs hooks` sets the repository's `core.hooksPath` to `.rstack/hooks/_`. Verify the installation with:

```bash
git config --get core.hooksPath
# .rstack/hooks/_
```

:::tip

- The `_` directory is generated dynamically and ignored by Git by default.
- If `rs hooks` detects another hooks path or existing Git hooks, it skips installation. Run `rs hooks --force` to install Rstack hooks anyway. See the [`rs hooks` guide](/guide/cli/hooks.md) for details.

:::

## Pre-commit checks

A `pre-commit` hook can lint and format the files staged for the current commit.

### Configure tasks

Add staged-file tasks to the Rstack config file. Adjust the glob patterns for the languages used by your project:

```ts title="rstack.config.ts"
import { define } from 'rstack';

define.staged({
  '*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}': ['rs lint --fix', 'rs fmt'],
  '*.{json,md,mdx,css,scss,less,html,yml,yaml}': 'rs fmt',
});
```

### Add the hook

Create `.rstack/hooks/pre-commit` and run `rs staged` from it:

```sh title=".rstack/hooks/pre-commit"
rs staged
```

### How it works

When you run `git commit`, Git invokes the hook installed by `rs hooks`. The hook executes `.rstack/hooks/pre-commit`, and `rs staged` then runs the configured tasks on the staged files.

`rs staged` passes matching staged files to each command. Commands in an array run in order: [`rs lint --fix`](/guide/cli/lint.md) first applies available fixes, then [`rs fmt`](/guide/cli/fmt.md) formats the result. Remove `--fix` if lint errors should block the commit without changing files.

After every task passes, the commit continues and includes the fixed and formatted results. If any task fails, the commit stops; fix the issue and then try again.
