'How to access commit message with Husky pre-commit hook?

My husky script:

  "husky": {
    "hooks": {
      "pre-commit": "sh ./tools/githooks/pre-commit.sh"
    }
  }

Let's say I am doing a git commit -m "I want that text". How can I access to my commit message within the shell script? I tried to echo $HUSKY_GIT_PARAMS and $HUSKY_GIT_STDIN within the shell script but no success



Solution 1:[1]

A pre-commit hook would not access the commit message, because the hook is triggered before the commit creation.

A commit-msg hook is the right hook for checking a commit message content.

And it is available with husky in 2019

"commit-msg": "echo $HUSKY_GIT_PARAMS"

Update 2020, as commented by galethil

HUSKY_GIT_PARAMS is removed in version 5.
Instead Git parameters should be used directly in scripts (e.g. $1)


Note, since 2019, commit c4e1ed1 (Dec. 2020, Husky v5.0.5) mentions:

Previous HUSKY_GIT_PARAMS environment variable is replaced by native params $1, $2, etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1