10

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?

5
  • 5
    The whole point of using -m is that you can skip opening the editor and define the message inline Commented Oct 20, 2015 at 12:26
  • 1
    I would forbid them to push directly into upstream master and request them to create pull requests instead. If a commit message doesn't fit the quality standards, reject the pull request. Commented Oct 20, 2015 at 12:26
  • You can probably keep your template in some file "template" and run "git commit -F template" in order to take the commit-msg from that file. (thats an alternate of your solution, but -m itself means that you want to skip editor) Commented Oct 20, 2015 at 12:34
  • You can use a precommit hook that only allow multiline commits messages. This way, using -m gets very tedious :) Commented Oct 20, 2015 at 12:40
  • (1) Well, the whole point of automation for a team is to make it ... automatic. The alternative is to ask by email or verbally all team members to follow the new instructions of commit message. (2) We still push right to git server without the review. Commented Oct 21, 2015 at 7:44

2 Answers 2

14

-e opens the editor.

git commit -m "message" -e 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.