Get commit message in Git hook


I would like to check the commit message before Git commit.

I use a pre-commit hook to do that, but I couldn’t find the way to get the commit message in the .git/pre-commit script.
How could I get it?


In the pre-commit hook, the commit message usually hasn’t been created yet 1. You probably want to use one of the prepare-commit-msg or commit-msg hooks instead. There’s a nice section in Pro Git on the order in which these hooks are run, and what you typically might do with them.

1. The exception is that the committer might have supplied a commit message with -m, but the message still isn’t accessible to the pre-commit hook, whereas it is to prepare-commit-msg or commit-msg

Answered By – Mark Longair

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published