Pre-commit

pre-commit is a framework for managing and maintaining multi-language pre-commit Git hooks.

Install

Install.

pip install pre-commit
pre-commit install
touch .pre-commit-config.yaml

Configure .pre-commit-config.yaml. See the examples below.

Run

Run against all files.

pre-commit run --all-files
Info

Use --all-files to run the Pre-commit scripts. Without it, Pre-commit looks for only changed files to run against.

Run a certain stage, e.g., push

pre-commit run --all-files --hook-stage push

Skip tests with --no-verify.

git push --no-verify

Examples

Calendar example

repos:
  - repo: local
    hooks:
      - id: default
        name: pre-commit check
        description: Run placeholder pre-commit check as a test
        language: system
        entry: echo
        args:
          - 'Running pre-commit check...'
        files: ''
      - id: poetry-test
        name: poetry-test
        description: Run tests with Poetry
        language: system
        entry: poetry
        args:
          - run
          - pytest
        types:
          - python
      - id: pytest
        name: pytest
        description: Run tests with pytest
        language: system
        entry: pytest
        args:
          - .
        types:
          - python
  - repo: <https://github.com/commitizen-tools/commitizen>
    rev: v2.20.0
    hooks:
      - id: commitizen
        stages: [commit-msg]

Sales AI Stellar example

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.21.2
    hooks:
      - id: gitleaks
  - repo: https://github.com/commitizen-tools/commitizen
    rev: v4.0.0
    hooks:
      - id: commitizen
        name: "Detect commit message style issues"
        description: "Use Commitizen to detect commit message style issues"
        stages: [commit-msg]
  - repo: https://github.com/psf/black
    rev: 24.10.0
    hooks:
      - id: black
        name: "Detect code style issues"
        description: "Use Black to detect code style issues"
        stages: [pre-push]

Automatically created by GitHub in a demo repo

name: Auto Assign
on:
  issues:
    types: [opened]
  pull_request:
    types: [opened]
jobs:
  run:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      pull-requests: write
    steps:
    - name: 'Auto-assign issue'
      uses: pozil/auto-assign-issue@v1
      with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          assignees: mattnorris
          numOfAssignee: 1
name: Proof HTML
on:
  push:
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: anishathalye/proof-html@v1.1.0
        with:
          directory: ./

Resources

Pre-commit
Interactive graph
On this page
Install
Run
Examples
Resources