There's more...

You can extend the functionality of the prepare-commit-msg hook easily, but you should bear in mind that the waiting time for fetching some information should be worth the benefits. One thing that is usually easy to check is a dirty work area.

Here, we need to use the git status command in the prepare commit message hook, and we need to predict whether we will have modified files after the commit:

  1. To check this, we need to have something staged for committing and some unstaged changes, as follows:
$ git status
On branch master
nothing to commit, working directory clean
  1. Now, modify the fishtank.txt file:
$ echo "saltwater" >> fishtank.txt
  1. Use git status --porcelain to check the work area:
$ git status --porcelain
M fishtank.txt ...

Get Git Version Control Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.