pre-commit
pre-commit is a multi-language framework for managing and running code-quality checks as Git hooks, so you can catch problems before changes are committed or pushed. You declare hooks in a .pre-commit-config.yaml file, and pre-commit runs them consistently for everyone on the team.
Installation and Setup
Install the tool and initialize it in a Git repository:
Create a .pre-commit-config.yaml file at the repository root to declare the hooks that should run.
Key Features
- Manages Git hook installation for multiple hook types via
pre-commit install --hook-type ...and related configuration. - Installs each hook in an isolated, language-appropriate environment and reuses installed environments from a cache for faster subsequent runs.
- Pins hook repositories and revisions in
.pre-commit-config.yamlfor reproducible results across machines and CI. - Runs only the relevant files during Git-hook execution. By default,
pre-commit runruns on currently staged files and supports scanning the whole codebase withpre-commit run --all-files. - Updates pinned hook revisions with a single command (
pre-commit autoupdate).
Usage
Create a .pre-commit-config.yaml for a project:
.pre-commit-config.yaml
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.9 # Pin a Ruff release
hooks:
- id: ruff-check
args: [--fix] # Apply safe fixes
- id: ruff-format
Install the Git hooks for the current repository and run them:
$ pre-commit install
$ pre-commit run --all-files
Related Resources
Tutorial
Python Code Quality: Best Practices and Tools
In this tutorial, you'll learn about code quality and the key factors that make Python code high-quality. You'll explore effective strategies, powerful tools, and best practices to elevate your code to the next level.
For additional information on related topics, take a look at the following resources:
By Leodanis Pozo Ramos • Updated May 27, 2026