I just want to be able to run it to see if the code in my working tree passes it, without actually attempting a commit.
8 Answers
Just run the pre-commit script through the shell:
bash .git/hooks/pre-commit 13 Comments
./.git/hooks/precommitpre-commit run --all-files, see my answer here.git add the files!There's a Python package for this available here. Per the usage documentation:
If you want to manually run all pre-commit hooks on a repository, run
pre-commit run --all-files. To run individual hooks usepre-commit run <hook_id>.
So pre-commit run --all-files is what the OP is after.
5 Comments
pre-commit run pylint --files common/main.pygit installed, otherwise you would get this error: An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory?. It is relevant since I wanted to create a GitLab job that runs pre-commit to check if the repo was clean.For a single file:
pre-commit run --files YOUR_FILENAME 1 Comment
In recent git releases, you can use the git hook run command to accomplish at least some of this, for example if you want to run the pre-commit hook:
git hook run pre-commit The documentation for this command is available here
2 Comments
git regardless of having the Python pre-commit tool installed.Just run git commit. You don't have to add anything before doing this, hence in the end you get the message no changes added to commit.
5 Comments
Skipped for all the commit hooks in that case. (might depend on which method / way you use for the pre-commit hooks, we use the Python package named pre-commit)git hook run <hook name>If you want to run only a specific pre-commit hook, you can do so by specifying its ID.
For example, I have the next configuration:
- repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.89.1 hooks: - id: terraform_fmt - id: terraform_validate - id: terraform_docs and I can run only terraform_fmt by command:
pre-commit run terraform_fmt
Comments
If you just run .git/hooks/pre-commit from the shell, the git related environment variables won't be set oup.
1 Comment
If using the python pre-commit package from pre-commit.com
To run on the working tree:
pre-commit, which is shorthand for pre-commit run
To run on all files
pre-commit run --all-files
To run an individual hook
pre-commit run <hook_id>, where the ID can be obtained by checking .pre-commit-config.yaml
To run on the last commit
pre-commit run --from-ref=$(git rev-parse HEAD^) --to-ref=HEAD
git hook run [--ignore-missing] <hook-name> [-- <hook-args>]!