Skip to content

Pre-Commit Hooks

Git Hooks

Git has hooks, which can run code on various events:

  • Prior to commiting code (pre-commit).
  • After committing code (post-commit).
  • After checking out a branch (post-checkout).
  • Before rebasing (pre-rebase).
  • Etc.

Pre-Commit Hook

  • Pre-commit Git hooks run checks before a commit is accepted.
  • Pre-commit is a package to automate the setup.
  • After config, issues such as syntax errors and security flaws are picked up.
  • Used mostly for formatting and linting.
  • Linting is checking code against a set of formatting rules and for syntax errors.

Pre-Commit (Python Tool)

  • Pre-commit is a Python tool for simplifying and applying Git pre-commit hooks.
  • Hooks can be configured via a YAML file, then applied on each attempted commit.
  • There are many hooks available from different sources.

Install it with:

pip install pre-commit

Add pre-commit-config.yaml

  • Add a pre-commit-config.yaml to your repo root.
  • A best practice config file, taken from FMTM:
  # Versioning: Commit messages & changelog
  - repo:
    rev: v3.13.0
      - id: commitizen
        stages: [commit-msg]

  # Lint / autoformat: Python code
  - repo:
    # Ruff version.
    rev: "v0.1.13"
      # Run the linter
      - id: ruff
        args: [--fix, --exit-non-zero-on-fix]
      # Run the formatter
      - id: ruff-format

  # Autoformat: YAML, JSON, Markdown, etc.
  - repo:
    rev: v3.1.0
      - id: prettier

  # Lint: Bash scripts
  - repo:
    rev: 2.1.1
      - id: bashate

  # Lint: Shell scripts
  - repo:
    rev: v0.9.0.6
      - id: shellcheck
        args: ["-x"]

  # Lint: Markdown
  - repo:
    rev: v0.38.0
      - id: markdownlint
        args: [--fix, --ignore,, --ignore, .github]

Note: the config above is for a monorepo configuration.

Your repo may not require both Python and JS code formatting.

Add Hooks


# Standard install for most hooks
pre-commit install

# Additional commit-msg hook (for the commitizen hook above)
pre-commit install --hook-type commit-msg

Now when you attempt to commit to the repo:

  • Code will be auto-formatted.
  • An error will show if linting fails.
  • An error will show if commit messages are in the wrong format.