I want to restrict the people whoever commits to have a specific commit message format, how do I do that?
For example: Pair_Name|Story_Number|Commit_Message
A single line is almost never sufficient for a commit message, so it is almost always better to write your commit message in an editor. The default editor, Vim, is not very user friendly but Git allows you to set the editor of your choice for writing commit messages (see how to Set Your Git Commit Message Editor ). 2. First line is the subject
In the same way you don’t end an email subject line with a period, your Git commit subject line does not need a period either (plus with a 50 character limit you’ll want to save every character you can). 3. Skip a line after the subject This visually separates the subject line from the body. 4. The rest of the commit is the body
However, before you can save changes in Git, you have to tell Git which changes you want to save as you might have made tons of edits. A great way to do that is by adding a commit message to identify your changes. This option sets the commit's message. This option automatically commits all (including new) tracked, modified or deleted files.
First line is the subject In the same way you summarize an email with a subject line, the first line of your commit should summarize your commit. The easiest way for me to write a commit message in the imperative mood is to imagine the words “This will” and then whatever comes next becomes my commit message subject line.
There is a pre-commit-msg
or commit-msg
hook, that you could use:
Git repos come with sample hooks, e.g. the sample commit-msg
hook under git/hooks/commit-msg.sample
catches duplicate Signed-off-by lines.
# This example catches duplicate Signed-off-by lines.
test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
To enable a hook, don't forget to make it executable.
Here's some fictional example, which would only accept commit messages of the london|120|something ...
and the like:
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
# $regex = /\[ref: (\d+)\]/
PAIRS = ["london", "paris", "moscow"] # only these names allowed
STORIES = "\d{2,4}" # story must be a 2, 3 or 4 digit number
MESSAGE = ".{5,}" # message must be at least 5 chars long
$regex = "( (#{PAIRS.join('|')})\|#{STORIES}\|#{MESSAGE} )"
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly"
exit 1
end
In usage:
$ git ci -m "berlin|120"
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|XX"
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|Looks good."
[master 853e622] london|120|Looks good.
1 file changed, 1 insertion(+)
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