Featured image of post How pre-commit Streamlines Development

How pre-commit Streamlines Development

Automate checks and enforce style before code reaches CI.

Preface

Git and CI/CD have become standard in collaborative development, yet inconsistent coding styles and missed conventions still creep in. pre-commit catches such issues early by running checks before each commit.

What is pre-commit?

pre-commit is a framework for managing Git hooks. It can format code, scan for secrets, and enforce project rules using built‑in or custom plugins.

Installation

1
pip install pre-commit

Configuration

Create a .pre-commit-config.yaml specifying the hooks to run. For example:

1
2
3
4
5
6
7
8
9
repos:
  - repo: https://github.com/psf/black
    rev: 24.3.0
    hooks:
      - id: black
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: end-of-file-fixer

Enable the hooks with pre-commit install. Now every git commit triggers the checks.

Integration with CI

Add pre-commit run --all-files to CI pipelines so that hooks run consistently on servers like GitHub or GitLab.

Conclusion

Running pre-commit locally saves CI resources and keeps a clean, consistent codebase.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy