Enforce commit message format in git

Venu Madhav picture Venu Madhav · Jun 8, 2016 · Viewed 12.8k times · Source

How can I just enforce a commit message format in Git?

Can this be set at a repository level so that everyone who creates a branch will have this enforcement?

Answer

VonC picture VonC · Jun 9, 2016

As described in the Git ProBook "Customizing Git - An Example Git-Enforced Policy", such enforcement would be set through a hook, more specifically, a server-side hook like an update one:

All the server-side work will go into the update file in your hooks directory. The update hook runs once per branch being pushed and takes three arguments:

  • The name of the reference being pushed to
  • The old revision where that branch was
  • The new revision being pushed

That is preferred to client-side hooks (like a pre-commit one) which:

  • have to be set manually by each user
  • can be bypassed

A server-side hook will, for a given repo, enforce any rule you want by rejecting a git push if your policy is not respected.

This assumes that you have control over the remote repo hosting server to which your users are pushing.

If you don't, you are back to client-side pre-commits hooks, which can be set through a git template, whose template directory can be shared amongst all users (starting with git 2.9, June 2016).