# Catch broken FACET records before you push, with a pre-commit hook

> Install a git pre-commit hook that runs the same FACET check as your pull requests, which confirms feature descriptions and code still point at each other, only on the change you are committing, so broken links never reach the shared branch.

Source: https://facet-framework.com/en/guides/stop-broken-commits-locally/  
Last updated: 2026-11-26T23:33:14+01:00

You tidy up a folder name, commit, push and switch to the next task. Ten minutes later CI is red: a record no longer matches its path, and the code still points at the old id. Now you context-switch back, fix it, push again, and wait for the pipeline a second time.

FACET keeps a short description of each feature, called a record, in a file next to your code, and a comment in the code, called an anchor, names the record it belongs to. Rename a folder, and the two can stop matching. By the end of this guide that commit never leaves your laptop. `facet setup precommit` installs a git pre-commit hook that runs `facet lint --staged`: it checks the change you are about to commit and refuses it when it introduces a broken record or anchor. Errors that were already there before your change do not stop you.

The commands below use a small example shop written in TypeScript, set up as in [One answer instead of three](https://facet-framework.com/en/guides/write-down-what-a-feature-does/). They work the same in any Git repository with FACET records.

## Put facet on your PATH

The hook calls `facet` by name. A plain `git commit` does not see the project's `node_modules/.bin`, so the development dependency from “One answer instead of three” is not enough. Install the same version globally:

```sh
npm install --global facet-framework
```

Check that your shell finds it outside `npx`:

```sh
facet version
```

The first line starts with `facet core`. Keep the global version the same as the one in `package.json`, so the hook and CI judge with the same rules.

## Install the pre-commit hook

Preview what will happen. Without options nothing is written:

```sh
npx facet setup precommit
```

```console
$ npx facet setup precommit
Dry run: would install ~/demo-shop/.git/hooks/pre-commit, which runs 'facet lint --staged' and fails the commit when 'facet' is not on PATH.
Run with --write to do it.
```

Then install it:

```sh
npx facet setup precommit --write
```

```console
$ npx facet setup precommit --write
Wrote ~/demo-shop/.git/hooks/pre-commit.
```

The hook is a short shell script in `.git/hooks/pre-commit`. Git does not version that folder, so each person on the team runs this step once in their clone.

## Try the commit that would break CI

Rename the record folder, as a quick tidy-up might:

```sh
git mv docs/facets/features/checkout/discounts docs/facets/features/checkout/discount-codes
git commit -m "Rename the discounts folder"
```

![Terminal output of git commit: two errors, the @facet: tag in src/checkout/discount.ts references checkout/discounts/redeem-code which has no record, and the record in discount-codes declares an id its path does not imply; 2 errors; the commit is not created.](https://facet-framework.com/guides/stop-broken-commits-locally/commit-refused-light.png)

*The hook refuses the commit and names both broken links.*

No commit was created. The record's `id` still says `checkout/discounts/redeem-code`, its new path implies `checkout/discount-codes/redeem-code`, and the anchor in the code points at an id that no longer has a record at the matching path. These are the same errors the pull request check would have reported ten minutes later.

To see the same result without committing, run the check the hook runs:

```sh
npx facet lint --staged
```

## Fix it and commit

A rename of a record is three changes that belong together: the folder, the `id` in the record and every `@facet:` anchor that names it. Here, the quick fix is to undo the rename:

```sh
git mv docs/facets/features/checkout/discount-codes docs/facets/features/checkout/discounts
```

Then commit something that does not break anything, for example a line added to `README.md`:

```sh
git add README.md
git commit -m "Mention free shipping in the README"
```

![Terminal output of git commit: the hook prints 1 record, 1 anchor checked, 0 errors, 0 warnings, then git creates the commit Mention free shipping in the README.](https://facet-framework.com/guides/stop-broken-commits-locally/commit-passes-light.png)

*A clean change passes the hook and is committed.*

The hook only judges what the commit introduces. If `HEAD` already contains an error, for example because someone committed with `--no-verify`, an unrelated commit still goes through, and `npx facet lint` on the whole repository keeps reporting the old error.

> **What this does not do**
>
> The hook runs on your machine only, and `git commit --no-verify` skips it. It does not replace the check in the pull
> request; it moves the same check earlier. It checks records and anchors, not your tests. Errors already in `HEAD` stay
> until someone fixes them; run `npx facet lint` to see them.

To remove the hook again:

```sh
npx facet setup precommit --remove
```

It deletes the file only if FACET wrote it.

## If the hook says something else

- **`facet pre-commit: 'facet' is not on PATH, so the staged records and anchors were NOT checked.`:** the commit was refused because the hook could not run the check. Install facet globally as in step 1, or check that the global npm folder is on your PATH. A GUI git client may use a different PATH than your terminal.
- **`npm error code EACCES` during the global install:** your npm installs global packages into a system folder you cannot write to. Follow npm's guide to change the default directory, or use a Node version manager; do not use `sudo`.
- **`husky is present, so no git hook file is written. Add this line to .husky/pre-commit:`:** your project manages hooks with husky. Add the printed line `facet lint --staged` to `.husky/pre-commit` yourself.
- **`… already exists and was not written by FACET, so it is left alone.`:** you already have a pre-commit hook. FACET prints the lines to add to it and changes nothing.

For more on catching broken changes before they reach others, read [“That line can go.” Are you sure?](https://facet-framework.com/en/solutions/regression-prevention/)

### Sources

- [FACET Core](https://github.com/facet-framework/core)
- [Git documentation: githooks, pre-commit](https://git-scm.com/docs/githooks#_pre_commit)
- [npm: resolving EACCES permissions errors when installing packages globally](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally)
