Reputation: 788
I am implementing the git template commit message for the team.
I've done it through .git/hooks/prepare-commit-msg
hook by
adding one line:
cat ".gitmessage" >> "$1"
Also I've configured the below:
git config --local commit.template .gitmessage
Well, the template feature works fine but only when git commit is called
without -m
flag.
Unfortunately, all the team members work flow is:
git add ...
git commit -m "Some message"
Question: How to force git to always open the editor to edit the message,
even when called by git commit -m ...
command?
Upvotes: 9
Views: 3782
Reputation: 26078
You can modify the commit-msg
pre-hook to achieve this. For instance, you can check the number of lines in the commit message (check example below); use some regular expressions to check if the template is respected; etc.
#!/bin/bash
# Check if the number of lines is greater than 3
commit_msg_file="${1}"
minimum_lines=3
num_lines=$(wc -l "${commit_msg_file}" | cut -f1 -d' ')
if (( $num_lines < $minimum_lines )); then
echo >&2 "Error!"
exit 1
fi
Check this and this for reference.
Upvotes: 1