Pre-commit Hooks#

Git hooks#

  • hooks are scripts

  • hooks are triggered by an event

  • hooks are located in the .git/hooks directory

  • hooks are local

Workflow#

touch file.txt
git add file.txt
# hooks will be triggered
git commit -m "my commit message"

pre-commit#

A framework for managing and maintaining pre-commit hooks (Website)

Installation#

pip install pre-commit
# Or
conda install -c conda-forge pre-commit
# Or
poetry add --dev pre-commit

Setup#

  • create a file named .pre-commit-config.yaml

  • write down the configuration:

repos:
-   repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
    -   id: black

Usage#

# Run once
pre-commit install
# Do your work

Aknowledgements#

Thanks to Mathias Schaub for creating this introduction to pre-commit.

Resources#