Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 630
Venu Avatar asked Jun 08 '16 17:06

Venu


People also ask

What is a commit message in git?

git commit -a. Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files (those that have been added with git add at some point in their history). git commit -m "commit message" A shortcut command that immediately creates a commit with a passed commit message.

Is git commit message mandatory?

Git requires a commit to have a comment, otherwise it wont accept the commit. You can configure a default template with git as your default commit message or can look up the --allow-empty-message flag in git.


Video Answer


2 Answers

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

like image 123
VonC Avatar answered Oct 05 '22 04:10

VonC


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

like image 26
eyarz Avatar answered Oct 05 '22 05:10

eyarz