Venu
Venu

Reputation: 1772

Enforce commit message format in git

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?

Upvotes: 43

Views: 32476

Answers (2)

eyarz
eyarz

Reputation: 186

Server-side Git hook is the solution for you, but there is also the overhead of setting and maintaining the hook (and usually, also the server).

If you are looking for a SaaS solution (for server-side Git hook) - this is exactly what Datree* can do for you.

*Disclaimer: I’m co-founder and product leader @ Datree, but I’m well-versed with this pain as an active open source contributor. I also wrote a blog post about this problem - How to Get More Out of Your Git Commit Message

Upvotes: 4

VonC
VonC

Reputation: 1324278

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).

Upvotes: 13

Related Questions