How can I manually run a Git pre-commit hook, without attempting a commit?
Asked Answered
B

5

185

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.

Barde answered 17/1, 2018 at 12:33 Comment(2)
Note: Git 2.36 (Q1 2022) will come with git hook run [--ignore-missing] <hook-name> [-- <hook-args>]!Fossick
Late to the party, but it's a popular question, yet percieved to be ambigious if you read the comments in the answers below. Please add a line or two describing if you're after native shell functionality or something like python based pre-commit from pre-commit.com.Mycosis
O
211

Just run the pre-commit script through the shell:

bash .git/hooks/pre-commit
Orvil answered 17/1, 2018 at 12:41 Comment(11)
Oh it's that easy. Also seems they are directly executable, so you can do ./.git/hooks/precommitBarde
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 hooksBurgett
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 added 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
J
119

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 use pre-commit run <hook_id>.

So pre-commit run --all-files is what the OP is after.

Janinajanine answered 17/3, 2019 at 22:44 Comment(5)
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.pyJacobs
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
G
41

For a single file:

pre-commit run --files YOUR_FILENAME
Grum answered 8/10, 2021 at 13:31 Comment(1)
You also need to specify hook e.g. pre-commit run trailing-whitespace --files path\to\fileAlderson
C
14

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.

Cinematography answered 28/4, 2020 at 18:34 Comment(3)
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 scanThreatt
Please delete this answer. It completely misses the all the points made in some of the more useful answers above.Mycosis
W
2

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

Workingman answered 9/11, 2023 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.