I am looking for a configuration in the .pre-commit-config.yaml
to exclude pre-commit from being run on certain branches, or to run it only on some branches. I don't know if this feature not implemented, or if I am missing it in the docs.
Thanks!
I am looking for a configuration in the .pre-commit-config.yaml
to exclude pre-commit from being run on certain branches, or to run it only on some branches. I don't know if this feature not implemented, or if I am missing it in the docs.
Thanks!
Git hooks are just shell scripts that are executed.
So in the script you can do something similar to this:
if [ $(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') = "main" ]; then
# Do checks for main here
fi
There’s a few different ways to accomplish this:
$ git switch -c no-pre-commit
Switched to a new branch 'no-pre-commit'
$ git rm .pre-commit-config.yaml
rm '.pre-commit-config.yaml'
$ git commit -m 'Remove pre-commit config'
No .pre-commit-config.yaml file was found
- To temporarily silence this, run `PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`
- To permanently silence this, install pre-commit with the --allow-missing-config option
$ pre-commit install --allow-missing-config
pre-commit installed at .git/hooks/pre-commit
$ git commit -m 'Remove pre-commit config'
`.pre-commit-config.yaml` config file not found. Skipping `pre-commit`.
[no-pre-commit 948c59a] Remove pre-commit config
1 file changed, 159 deletions(-)
delete mode 100644 .pre-commit-config.yaml
If you’ve installed pre-commit for different stages, then you also have to run
pre-commit install -t <stage> --allow-missing-config
Technically, the only thing you need for a pre-commit config to be valid is a repos
list, and that list can be empty. If you add a commit to certain branches that changes their .pre-commit-config.yaml
to this,
repos: []
then pre-commit will do nothing when you commit to those branches.