Exclude multiple files in pre-commit-hooks
Update, October 2026: I'm keeping this article as I wrote it in 2023. Some things have changed since then, so read Before You Try This Today before you copy the commands.
How do you list all the files to avoid during pre-commit-hooks?
The issue
I have 2 configuration files in 2 different dockerized app.
I am using pre-commit-hooks to check, among other things, the Yaml files in my project.
The 2 files I try to exclude are: notes-app/mkdocs.yml and blog-app/mkdocs.yml.
So I tried to list them in the config file, .pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
exclude:
- notes-app/mkdocs.yml
- blog-app/mkdocs.yml
which of course did not work:
❯ git commit -m "blog-post: about pre-commit-hooks multiple exclusions"
An error has occurred: InvalidConfigError:
==> File .pre-commit-config.yaml
==> At Config()
==> At key: repos
==> At Repository(repo='https://github.com/pre-commit/pre-commit-hooks')
==> At key: hooks
==> At Hook(id='check-yaml')
==> At key: exclude
=====> Expected string got list
Check the log at /Users/cruder/.cache/pre-commit/pre-commit.log
Solution
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-yaml
exclude: ^(notes-app/mkdocs\.yml|blog-app/mkdocs\.yml)$
The exclude key should be a single string, not a list.
In order to resolve the issue and still keep it on one line I had to create a conditional in...REGEX.
Before You Try This Today
Added on 2026-10-20. The article above is unchanged since 2023-10-09; these are the gotchas you may hit if you follow it today.
-
These blocks are YAML, not shell. Tag them
```yamlif you copy them into your own docs, so they get the right highlighting. -
For a long list, use pre-commit's verbose regex. The docs suggest a YAML multi-line string with the
(?x)flag, one path per line:- id: check-yaml exclude: | (?x)^( notes-app/mkdocs\.yml| blog-app/mkdocs\.yml )$ -
excludeis matched withre.search. Without the^...$anchors, a pattern also matches inside longer paths:mkdocs\.ymlalone would excludeold-mkdocs.ymltoo. Keep the anchors and escape the dots. -
You may not need to exclude these files at all. The usual reason
check-yamlrejectsmkdocs.ymlis the!!python/name:tags that Material for MkDocs uses.args: [--unsafe]on the hook checks syntax only and accepts custom tags, so the files are still checked. -
excludeis a pre-commit setting, not a pre-commit-hooks one. It works the same way on any hook from any repo, and there's also a top-levelexcludefor the whole config. -
Pin
revto a current release withpre-commit autoupdate, rather than copying the version shown here.
References
- pre-commit: Regular expressions:
files/excludeare Python regexes matched withre.search, and the(?x)multi-line example. - pre-commit-hooks:
check-yaml: the--unsafeand--allow-multiple-documentsoptions.
Links checked on 2026-10-20.