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.
How can I manually run a Git pre-commit hook, without attempting a commit?
Just run the pre-commit
script through the shell:
bash .git/hooks/pre-commit
Oh it's that easy. Also seems they are directly executable, so you can do
./.git/hooks/precommit
–
Barde Yes, it is also directly executable. –
Orvil
This won't detect/correct all problems in all existing files - for that, you want
pre-commit run --all-files
, see my answer here. –
Janinajanine If you find that this doesn't do anything, don't forget to
git add
the files! –
Kohler @Janinajanine note that this answer does not refer to the python pre-commit package but rather the native git pre-commit hook. Your command will not work for someone who just uses native git hooks –
Burgett
This is perfect, also works for any hook, like pre-push hook ! Thanks ! –
Flofloat
Note: you need to be in your repository root dir for this to work. E.g. If you're in a subdirectory and do
../.git/hooks/pre-commit
, it won't recognize the files you've add
ed to your git workspace. –
Cursed This answer assumed that
pre-commit
is a bash shell script. It doesn't have to be. –
Immovable .git/hooks/pre-commit must be a bash script: git-scm.com/book/en/v2/Git-Internals-Environment-Variables "Git always runs inside a bash shell" –
Mycosis
Answer does not work in the case of working on a submodule. Executing via
pre-commit
per @Janinajanine does. –
Apterous 1 more note: This .git/hooks/pre-commit does only exist once per worktree, so if you use this feature and your are not on your "main" worktree: go and find your .git/hooks/pre-commit in your main worktree. –
Commodity
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.
Note this pre-commit isn't the git pre-commit. Rather, it's a python package from pre-commit.com that installs a git pre-commit script. However, the python package is what I came here looking for, so upvote for you. –
Cullis
The alternative way is to specify pre-commit hook (pylint in this case) and file need to be checked:
pre-commit run pylint --files common/main.py
–
Jacobs This was the right answer for my google search, but the OP question was ambiguous. –
Draco
Note that you need to have
git
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. –
Phillip Note that if you have hooks which modify files (such as black) then you will potentially end up modifying a ton of files you didn't intend to with this command. You probably want those changes eventually, but not half way through an existing commit. –
Nibelung
For a single file:
pre-commit run --files YOUR_FILENAME
You also need to specify hook e.g. pre-commit run trailing-whitespace --files path\to\file –
Alderson
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
.
At least for me it just says
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
) –
Delubrum @Delubrum by default it will run only on changed files, I think. Run pre-commit run --all-files to confirm you can enforce the full repo scan –
Threatt
Please delete this answer. It completely misses the all the points made in some of the more useful answers above. –
Mycosis
In recent git releases, you can use the git hook run command to accomplish at least some of this. Check out the command at enter link description here
© 2022 - 2024 — McMap. All rights reserved.
git hook run [--ignore-missing] <hook-name> [-- <hook-args>]
! – Fossick