Pular para o conteúdo

Integrate with your VCS

Este conteúdo não está disponível em sua língua ainda.

The VCS (Version Control System) integration gives Biome additional features that only a VCS can provide, such as reading your ignore files and finding the files you changed. Biome supports only Git, and the integration is opt-in.

To enable the integration, set vcs.enabled and vcs.clientKind in the Biome configuration file:

biome.json
{
"vcs": {
"enabled": true,
"clientKind": "git"
}
}

This configuration does not change anything on its own. Opt in to the features you want, as described in the following sections.

Enable vcs.useIgnoreFile to make Biome ignore all the files and directories listed in the project’s VCS ignore files, as well as in .ignore files.

For Git repositories, Biome reads .gitignore files and Git’s local exclude file at .git/info/exclude. In linked worktrees, Biome reads .git/info/exclude from the common Git directory, matching Git’s own behavior.

biome.json
{
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}

The --changed option processes only the files that have changed from one revision to another. It is available only in the CLI.

First, set the default branch in the vcs.defaultBranch field of your configuration file:

biome.json
{
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true,
"defaultBranch": "main"
}
}

Then, add the --changed option to your command. Biome asks the VCS which files changed between the main branch and your current revision, and processes only those files:

npx biome check --changed

--changed compares only committed changes. It does not include staged or unstaged changes, so it is intended for CI. To check your changes locally before you commit them, process only staged files instead.

To compare against another branch, use the --since option. It takes precedence over vcs.defaultBranch. For example, to check your changes against the next branch:

npx biome check --changed --since=next

Before you commit your changes, you can check formatting and linting for the files added to the index (also known as staged files), without running Biome on the entire project.

To process only the files that you are about to commit, pass the --staged option to the command:

npx biome check --staged

To also format, lint, sort imports, and apply safe fixes to them, add --write:

npx biome check --write --staged

The --staged option is not available in the ci command, because you usually do not commit changes during CI. To run it before every commit, use a Git hook.

Git hooks run scripts during Git commands, for example to format and lint the files you staged before you commit or push them. You can write the hook script yourself, or call Biome from one of the common hook managers.

Most examples in this section also pass two flags:

  • --files-ignore-unknown=true skips files that Biome does not support, so the hook handles every file type that Biome supports today and in later versions.
  • --no-errors-on-unmatched prevents an error when no files are processed, for example when you only staged files that Biome ignores.

Shell scripts can behave differently across platforms, so a dedicated tool such as Lefthook or pre-commit is more reliable.

To check your staged files before every commit, save this script as .git/hooks/pre-commit:

.git/hooks/pre-commit
#!/bin/sh
set -eu
npx @biomejs/biome check --staged --files-ignore-unknown=true --no-errors-on-unmatched

To also apply safe fixes, and stage them:

.git/hooks/pre-commit
#!/bin/sh
set -eu
if git status --short | grep --quiet '^MM'; then
printf '%s\n' "ERROR: Some staged files have unstaged changes" >&2
exit 1;
fi
npx @biomejs/biome check --write --staged --files-ignore-unknown=true --no-errors-on-unmatched
git update-index --again

This hook fails if a staged file also has unstaged changes, because git update-index --again would stage those changes along with the fixes.

Lefthook is a fast, cross-platform hook manager without dependencies.

  1. Install Lefthook, for example with npm.

  2. Create lefthook.yml at the root of your Git repository. This configuration checks formatting and linting before every commit:

    lefthook.yml
    pre-commit:
    commands:
    check:
    run: npx @biomejs/biome check --no-errors-on-unmatched --files-ignore-unknown=true {staged_files}
  3. Run lefthook install to set up the hooks.

To format, lint, and apply safe fixes before every commit, add --write. Then, stage_fixed: true adds the fixed files back to the staging area:

lefthook.yml
pre-commit:
commands:
check:
run: npx @biomejs/biome check --write --no-errors-on-unmatched --files-ignore-unknown=true {staged_files}
stage_fixed: true

To check formatting and linting before every push instead, use the pre-push hook and the {push_files} placeholder:

lefthook.yml
pre-push:
commands:
check:
run: npx @biomejs/biome check --no-errors-on-unmatched --files-ignore-unknown=true {push_files}

To process only some files, add a Lefthook glob, such as glob: "*.{js,ts,jsx,tsx}", to the command. You then do not need --files-ignore-unknown=true.

Husky is a widely used hook manager in the JavaScript ecosystem. Husky does not hide unstaged changes and cannot provide the list of staged files, so it is often used together with lint-staged or git-format-staged.

If your project contains a package.json, you can set up the Husky hooks every time you install your packages with scripts.prepare:

package.json
{
"scripts": {
"prepare": "husky"
}
}

lint-staged runs commands on your staged files, and is one of the most used tools in the JavaScript ecosystem.

  1. Add the following Husky hook:

    .husky/pre-commit
    lint-staged
  2. Configure lint-staged in package.json. This configuration formats, lints, sorts imports, and applies safe fixes to every staged file that Biome supports:

    package.json
    {
    "lint-staged": {
    "*": [
    "biome check --write --no-errors-on-unmatched --files-ignore-unknown=true"
    ]
    }
    }

lint-staged runs every command in the array, so each entry adds a step instead of offering an alternative. To change what the hook does, adjust the command:

  • Remove --write to only report formatting and lint problems, without changing your files.
  • Add --unsafe to also apply unsafe fixes.
  • Add --assist-enabled=false to format and apply safe fixes without running assist actions such as import sorting.
  • Use biome format --write to only format, or biome lint --write to only lint and apply safe fixes.
  • Replace "*" with a glob such as "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc,css}" to process only some files. You then do not need --files-ignore-unknown=true.

Keep --no-errors-on-unmatched in every command, so that the hook does not fail when no files are processed.

Unlike Lefthook, pre-commit, and lint-staged, git-format-staged does not use git stash internally. This avoids manual intervention when your unstaged changes conflict with the updated staged changes. See the comparison of git-format-staged with other tools.

To check formatting and linting before every commit:

.husky/pre-commit
git-format-staged --formatter 'biome check --files-ignore-unknown=true --no-errors-on-unmatched --stdin-file-path="{}"' '*'

To format, lint, and apply safe fixes before every commit:

.husky/pre-commit
git-format-staged --formatter 'biome check --write --files-ignore-unknown=true --no-errors-on-unmatched --stdin-file-path="{}"' '*'

pre-commit is a hook manager for projects in any language. Biome provides four hooks in the biomejs/pre-commit repository:

Hook ID Description
biome-ci Checks formatting, import sorting, and lint rules.
biome-check Formats, sorts imports, lints, and applies safe fixes to the committed files.
biome-format Formats the committed files.
biome-lint Lints and applies safe fixes to the committed files.
  1. Install pre-commit.

  2. Create .pre-commit-config.yaml at the root of your project. This configuration uses the biome-check hook, which runs biome check --write every time you run git commit:

    .pre-commit-config.yaml
    repos:
    - repo: https://github.com/biomejs/pre-commit
    rev: "v2.6.10"
    hooks:
    - id: biome-check

    Each tag of the repository installs the Biome version with the same number. To upgrade to the latest version, run pre-commit autoupdate.

  3. Run pre-commit install in your repository.

If your project already installs Biome as an npm package, you then have to update both package.json and .pre-commit-config.yaml every time you upgrade Biome. To avoid this, use a local hook instead of the hooks that Biome provides. For example, if you use npm:

.pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: local-biome-check
name: biome check
entry: npx @biomejs/biome check --write --files-ignore-unknown=true --no-errors-on-unmatched
language: system
types: [text]
files: "\\.(jsx?|tsx?|c(js|ts)|m(js|ts)|d\\.(ts|cts|mts)|jsonc?|css)$"

The files option is optional, because --files-ignore-unknown=true already makes Biome skip the files it does not support.