0

How to read commit message from git commit -m "message" using pre-commit hook. Or Is there any hook that reads commit message.

commit-msg hook can read the message but we need to provide a text file as input which I don't want.

  • 3
    Possible duplicate of [get commit message in git hook](https://stackoverflow.com/questions/5393178/get-commit-message-in-git-hook) – Jonas Praem Feb 05 '19 at 08:09
  • What are you ultimately trying to achieve? pre-commit may just be the wrong tool for the job. If you want to evaluate the commit message, `commit-msg` is the more natural place for it. – Mureinik Feb 05 '19 at 08:11
  • You won't *need* to provide a text file as input, it is a possible feature but message template files are in no way mandatory. – Romain Valeri Feb 05 '19 at 08:17
  • @Mureinik I need to check for errors in commit message. I can check with commit-msg hook, but then my commit message changes to git commit -F filename. – Kallempudi Goutham Feb 05 '19 at 08:22
  • @RomainValeri Thanks it worked for me – Kallempudi Goutham Feb 05 '19 at 08:48
  • pre-commit hook is executed before the commit, so naturally it cannot access the commit message since it doesn't exist yet. – 1615903 Feb 05 '19 at 10:25

2 Answers2

5

You can use the commit-msg hook. The $1 argument there contains the commit message, and if the check you need to perform fails, just return 1 to fail the commit.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Once git commit -m "message" has been invoked, the message part gets stored in .git/COMMIT_EDITMSG file and commit-msg hook takes this as the input internally.

so you can use git commit -m "message" instead of git commit -F Filepath.

origin
  • 120
  • 2
  • 8