Skip to content

Guide · Open source

How to use pre-commit: set up git commit hooks

Updated · written with AI from the project's own documentation and checked against our catalogue data

pre-commit is an open-source framework for managing and maintaining pre-commit hooks, which are checks such as linters or formatters that run automatically before a git commit is saved. This guide explains what the tool is for, what you need before starting, and how it fits into a project. It is written for developers and maintainers who want consistent checks on every commit, including projects that use several programming languages.

pre-commitpre-commit/pre-commit · ★ 15.7k · MIT

Quick start

  1. 1Check prerequisites. Confirm that Python and git are installed and that your project is a git repository.
  2. 2Install pre-commit. Install pre-commit with a package manager, following the current instructions at pre-commit.com.
  3. 3Choose your hooks. Pick the linters or formatters you want to run before each commit.
  4. 4Configure the hooks. Add your hook configuration to the repository as described in the pre-commit documentation.
  5. 5Install the hooks into git. Activate the hooks so they run automatically when you make a commit.
  6. 6Commit and review results. Make a commit and check the output of the hooks before the commit is saved.

What pre-commit does

Pre-commit hooks are checks that run automatically before a git commit is saved. Typical examples named in the project's own description are linters and formatters. pre-commit is the framework that manages and maintains these hooks for you.

A key feature is support for hooks written in multiple languages, so one tool can coordinate checks from different ecosystems in the same project.

What you need before you start

pre-commit is built around git commits, so your project must use git. The tool is written in Python, so Python must be available on your machine.

The README is brief and points to the project website at https://pre-commit.com/ for full documentation, including setup details.

  • Python
  • git
  • A project that uses git commits

Install pre-commit

pre-commit is installed through a package manager. The project README does not list specific install commands, so check the documentation at https://pre-commit.com/ for the current instructions for your system.

Set up hooks for your repository

Once pre-commit is installed, you configure which hooks run for your repository and then install them so they run on each commit. The README describes this as setting up ongoing hooks rather than manual checks.

Because the framework is designed to be shared, a single standard set of hooks can be applied across several repositories, which helps teams keep checks consistent.

  • Choose the checks (linters or formatters) you want on every commit
  • Apply the same hook set across repositories if your team maintains several

Decide if pre-commit fits your project

pre-commit suits teams that want consistent checks on every commit and developers working across several languages. It is less suited to a one-off lint run, because it sets up ongoing hooks rather than manual checks.

Comparable projects include husky, lefthook, git-hooks and Overcommit. Compare them on the languages and workflows you need.

What people use it for

  • Run the same linters and formatters on every commit so code style stays consistent across a team
  • Coordinate checks written in different languages within one multi-language project
  • Share a standard set of hooks across several repositories maintained by the same team

Frequently asked questions

Is pre-commit free to use?

pre-commit is released under the MIT licence, as stated in the project information. Check the LICENSE file in the repository for the full terms.

What do I need to run pre-commit?

The project lists Python and git as prerequisites. The tool is built around git commits, so it is intended for projects that use git.

How do I install pre-commit?

The project is installed through a package manager. The README does not list exact commands, so follow the current instructions at https://pre-commit.com/.

Can pre-commit run hooks written in different languages?

Yes. The project describes itself as a framework for multi-language pre-commit hooks, so one tool can coordinate checks from different ecosystems.

Is pre-commit good for one-off lint runs?

Not really. The tool is designed to set up ongoing hooks that run on commits, so it is a poor fit if you only want to run a lint check manually once.

What are alternatives to pre-commit?

Comparable projects include husky, lefthook, git-hooks and Overcommit. Each handles git hooks differently, so compare them against your language and workflow needs.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

pre-commit is a good choice for teams that want consistent checks on every git commit, especially in projects that mix several languages or share hooks across repositories. If you only need a one-time lint run or do not use git, pick a different approach.

Install options & downloads