pre-commit hooks are a way to add hooks that run before the commit process. They are a great way to make sure your code is ready to be committed.

Installation

Follow platform-specific instructions for installing pre-commit. Or if you have pip installed in your machine. Run:

pip install pre-commit

Initialization

Run the following command to initialize the git hook:

pre-commit install

Configuration

Add a .pre-commit-config.yaml config file in the root folder. Here’s an example:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    # Ruff version.
    rev: v0.3.4
    hooks:
      # Run the linter.
      - id: ruff
        args: [--fix]
      # Run the formatter.
      - id: ruff-format