Setting default commit template in Git

Well-formed commit messages can be rather helpful in terms of tracking the history or generating the changelog automatically. Here we will configure a commit template to help writing commit messages in a good format. You can force it across the whole team.

Configure Git commit message template

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

<type>: subject (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 starting with a commit type, for example feat: add settings page (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.

Commit types

The commit types known to developers that you can set:

Commit Type Description
feat New feature.
fix Fix a bug.
refactor Refactor production code.
style Format issues like missing semi colons, etc, no code change.
docs Change documentation files.
test Add or refactor tests, no production code change.
chore Update grunt tasks, no production code change. Some examples of grunt tasks: configuration changes (.gitignore, .gitattributes), tool changes or something that would not go into the product.

You can put commit types in the template file to help to pick the proper commit type as Git commit template shows:

# <type>: (If applied, this commit will...) <subject> (Max 50 char)
# |<----  Using a Maximum Of 50 Characters  ---->|


# Explain why this change is being made
# |<----   Try To Limit Each Line to a Maximum Of 72 Characters   ---->|

# Provide links or keys to any relevant tickets, articles or other resources
# Example: Github issue #23

# --- COMMIT END ---
# Type can be
#    feat     (new feature)
#    fix      (bug fix)
#    refactor (refactoring production code)
#    style    (formatting, missing semi colons, etc; no code change)
#    docs     (changes to documentation)
#    test     (adding or refactoring tests; no production code change)
#    chore    (updating grunt tasks etc; no production code change)
# --------------------
# Remember to
#   - Capitalize the subject line
#   - Use the imperative mood in the subject line
#   - Do not end the subject line with a period
#   - Separate subject from body with a blank line
#   - Use the body to explain what and why vs. how
#   - Can use multiple lines with "-" for bullet points in body
# --------------------
# For updated template, visit:
# https://gist.github.com/adeekshith/cd4c95a064977cdc6c50
# Licence CC

Resources