callum
callum

Reputation: 37709

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

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.

Upvotes: 212

Views: 192159

Answers (7)

dylanmorroll
dylanmorroll

Reputation: 525

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

Upvotes: 0

Randall Becker
Randall Becker

Reputation: 201

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

Upvotes: 11

nmishin
nmishin

Reputation: 3038

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

Upvotes: 4

Ilayaraja
Ilayaraja

Reputation: 2876

Just run the pre-commit script through the shell:

bash .git/hooks/pre-commit

Upvotes: 232

Martin
Martin

Reputation: 1003

For a single file:

pre-commit run --files YOUR_FILENAME

Upvotes: 47

npnmodp
npnmodp

Reputation: 1232

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.

Upvotes: 14

javabrett
javabrett

Reputation: 7618

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.

Upvotes: 141

Related Questions