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
sed
by passing --show-current
to git branch
. –
Eutectoid 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.
© 2022 - 2024 — McMap. All rights reserved.
.pre-commit-config.yaml
is not part ofgit
alone, it must be part of an extra tool :gitlab
orjenkins
orredmine
or ... Can you specify which one ? – Outride.pre-commit-config.yaml
works in local (and it does indeed after I add or remove hooks). @LasseV.Karlsen this is what my question refers to. I would like pre-commit not to be run on certain branches. This configuration is available in overcommit, but I don't find it for pre-commit. – Flewpre-commit
quite often. Would be nice to be able to run certain steps on given branches only. This way you could make sure the easy stuff like formatting and doctests is still active, while type-hinting can be refactored later. Even better would be a way to deactivate certain steps in a commit message, which are then forbidden on official branches likemain
. – Garling