Setting default commit template in Git

Well-formed commit messages can be rather helpful when tracking the history. Configure a commit template to help you write commit messages in good format and follow it across the whole team.

Write you own commit template file, such as ~/.gitmessage. Below is a good example :

Subject line (try to keep under 50 characters)

Multi-line description of commit to explain
what, why and how this change is being made
(try to limit each line under 72 Characters)

Provide ticket numbers or links to other relevant resource
Example:
[Ticket: #53]

This template structure contains 3 sections:

  1. head, a subject line (followed by a blank line)
  2. body, explains the commit
  3. foot, optional, provides tickets or other information

Setup it as the default commit template with below command:

# Config a custom commit template
$ git config --global commit.template ~/.gitmessage

When you run git commit command, an editor opens containing something like this.

Subject line (try to keep under 50 characters)

Multi-line description of commit to explain
what, why and how this change is being made
(try to limit each line under 72 Characters)

Provide links or ids to relevant issues or other resources
Example:
[Ticket: #53]
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   my-plugin.php
#

Then replace the 3 parts with your actual content.