# \[ANN\] ocamlformat pre-commit hook

**URL:** <https://discuss.ocaml.org/t/ann-ocamlformat-pre-commit-hook/5602>\
**Category:** Ecosystem\
**Tags:** announce\
**Created:** [April 23, 2020, 9:35pm UTC](https://discuss.ocaml.org/t/ann-ocamlformat-pre-commit-hook/5602 "2020-04-23T21:35:43Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![BrendanLong](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/brendanlong/32/522_2.png) [@BrendanLong](https://discuss.ocaml.org/u/BrendanLong)\
**Post date:** [April 23, 2020, 9:35pm UTC](https://discuss.ocaml.org/t/ann-ocamlformat-pre-commit-hook/5602/1 "2020-04-23T21:35:44Z")

</div>

This is kind of trivial but I figured it might be useful for other people. We created a hook config for using [ocamlformat](https://github.com/ocaml-ppx/ocamlformat#ocamlformat) with [pre-commit](https://pre-commit.com/):

> **[arenadotio/pre-commit-ocamlformat](https://github.com/arenadotio/pre-commit-ocamlformat)**
>
> ocamlformat hook for pre-commit. Contribute to arenadotio/pre-commit-ocamlformat development by creating an account on GitHub.

pre-commit is a tool that makes it easier to run checks on changed files before commiting them, and this makes it so you can auto-run ocamlformat and ensure no unformatted code gets into your repo.

1. [Install pre-commit](https://pre-commit.com/#install) like `pip install pre-commit`
2. In your repo, add a .pre-commit-config.yaml like:

```auto
---
repos:
  - repo: https://github.com/arenadotio/pre-commit-ocamlformat
    rev: master # or pick a commit sha I guess
    hooks:
     - id: ocamlformat

```

3. Run `pre-commit install`
4. Now every time you run `git commit` (or `pre-commit run`), it will run every staged OCaml file through ocamlformat and complain if there are any changes:

```bash
$ pre-commit run ocamlformat
ocamlformat.....Failed
- hook id: ocamlformat
- files were modified by this hook
$ git add .
$ pre-commit run ocamlformat
ocamlformat.....Passed

```

---

_[View the full topic](https://discuss.ocaml.org/t/ann-ocamlformat-pre-commit-hook/5602)._
