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?
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.
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.
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. Theupdate
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:
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With