223

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.

2
  • 5
    Note: Git 2.36 (Q1 2022) will come with git hook run [--ignore-missing] <hook-name> [-- <hook-args>]! Commented Feb 13, 2022 at 14:45
  • 1
    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. Commented Feb 21, 2023 at 10:52

8 Answers 8

239

Just run the pre-commit script through the shell:

bash .git/hooks/pre-commit 
Sign up to request clarification or add additional context in comments.

13 Comments

Oh it's that easy. Also seems they are directly executable, so you can do ./.git/hooks/precommit
Yes, it is also directly executable.
This won't detect/correct all problems in all existing files - for that, you want pre-commit run --all-files, see my answer here.
If you find that this doesn't do anything, don't forget to git add the files!
Is there a way to run git hooks on all files without actually adding them to the git index?
@javabrett 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
|
146

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.

5 Comments

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.
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
This was the right answer for my google search, but the OP question was ambiguous.
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.
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.
49

For a single file:

pre-commit run --files YOUR_FILENAME 

1 Comment

You also need to specify hook e.g. pre-commit run trailing-whitespace --files path\to\file
27

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

This should be the accepted answer as its the idiomatic way of doing it in a way that works with just git regardless of having the Python pre-commit tool installed.
It is also the only way to support installations that have a core.hookspath configured.
12

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

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)
@mozzbozz 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
Please delete this answer. It completely misses the all the points made in some of the more useful answers above.
@Jepper all the answers above are either talking about the Python pre-commit tool or about invoking bash. And in my case bash is not installed when I am doing tests on a Windows VM, so this is actually the only one that worked for me on all different OSs, which is what I needed.
That said Randall Becker's answer below is probably the best answer, as now git supports the command git hook run <hook name>
3

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

0

If you just run .git/hooks/pre-commit from the shell, the git related environment variables won't be set oup.

1 Comment

This says exactly the same as the accepted answer. Please upvote answers you find useful, instead of repeating it.
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.