158

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
callum
  • 34,206
  • 35
  • 106
  • 163
  • 3
    Note: Git 2.36 (Q1 2022) will come with [`git hook run [--ignore-missing] [-- ]`](https://stackoverflow.com/a/71101716/6309)! – VonC Feb 13 '22 at 14:45
  • 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. – Jepper Feb 21 '23 at 10:52

4 Answers4

185

Just run the pre-commit script through the shell:

bash .git/hooks/pre-commit
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ilayaraja
  • 2,388
  • 1
  • 9
  • 9
  • 17
    Oh it's that easy. Also seems they are directly executable, so you can do `./.git/hooks/precommit` – callum Jan 17 '18 at 12:43
  • 2
    Yes, it is also directly executable. – Ilayaraja Jan 17 '18 at 12:44
  • 13
    This won't detect/correct all problems in all existing files - for that, you want `pre-commit run --all-files`, see my answer here. – javabrett Mar 17 '19 at 22:45
  • 3
    If you find that this doesn't do anything, don't forget to `git add` the files! – Ollie Dec 31 '19 at 00:24
  • 4
    @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 – bjrne May 03 '20 at 14:18
  • This is perfect, also works for any hook, like pre-push hook ! Thanks ! – TBG Oct 26 '20 at 08:29
  • 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. – broofa Nov 20 '21 at 15:52
  • This answer assumed that `pre-commit` is a bash shell script. It doesn't have to be. – gerrit Nov 25 '21 at 16:08
  • .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" – Jepper Feb 21 '23 at 10:51
  • Answer does not work in the case of working on a submodule. Executing via `pre-commit` per @javabrett does. – dotHTM Mar 17 '23 at 16:45
101

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
javabrett
  • 7,020
  • 4
  • 51
  • 73
  • 17
    Note this pre-commit isn't the git pre-commit. Rather, it's a python package from https://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. – dfrankow Jun 27 '19 at 18:53
  • 3
    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` – klapshin Jun 07 '21 at 12:31
  • This was the right answer for my google search, but the OP question was ambiguous. – yellow-saint Aug 30 '22 at 16:36
  • 1
    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. – vvvvv Sep 07 '22 at 13:39
30

For a single file:

pre-commit run --files YOUR_FILENAME
Martin
  • 659
  • 7
  • 14
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.

Yichun
  • 1,192
  • 9
  • 15
  • 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`) – mozzbozz Jan 11 '21 at 10:04
  • 1
    @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 – Maciej Skorski Nov 10 '22 at 09:28
  • 3
    Please delete this answer. It completely misses the all the points made in some of the more useful answers above. – Jepper Feb 21 '23 at 10:38