how to write git commit message

Introduction

The git commit message is very important. You can use commit message to generate CHANGELOG in your ci pipeline Or any you want to do something.
So, We can standardize standard commit message for you or your team.

Installation

We can help ourselves to write beautiful message through some tools.

1
brew install commitizen pre-commit

Specification

The template include three part, header, body and footer.

Header: <type>(<scope>): <subject>

type list:

  • feat: A new feature
  • fix: A bug fix
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
  • chore: Other changes that don’t modify src or test files
  • revert: Reverts a previous commit

Body: 72-character wrapped.

  • The body is a detailed description of this submission, which can be multiple lines, and each line should not exceed 72
    characters
  • Explain the items and reasons of the code changes, as well as the comparison with the previous behavior

Footer:

  • Fill in the task number (if any)
  • BREAKING CHANGE (can be ignored), record incompatible changes, start with BREAKING CHANGE:, followed by a description
    of the change, the reason for the change, and the migration method.

Set commit message template

Create a file call .gitmessage.txt in $HOME directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Header
# <type>(<scope>): <subject>

# type list:
# feat: A new feature
# fix: A bug fix
# docs: Documentation only changes
# style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
# refactor: A code change that neither fixes a bug nor adds a feature
# perf: A code change that improves performance
# test: Adding missing tests or correcting existing tests
# build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
# ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
# chore: Other changes that don't modify src or test files
# revert: Reverts a previous commit

# Body
# 72-character wrapped.

# Footer
# Fill in the task number (if any)
# https://docs.gitlab.com/ee/integration/jira/index.html

Set git config template location

1
git config --global commit.template ~/.gitmessage.txt

Using commitizen

Initialize commitizen and pre-commit to standardize commit message when git commit.

1
cz init

And then you should get two files .cz.yaml and .pre-commit-config.yaml.

  • .cz.yaml
1
2
3
4
commitizen:
name: cz_conventional_commits
tag_format: $version
version: 0.0.1
  • .pre-commit-config.yaml
1
2
3
4
5
6
7
repos:
- repo: https://github.com/commitizen-tools/commitizen
rev: v2.20.5
hooks:
- id: commitizen
stages:
- commit-msg

You can create a new tag via cz bump or other commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
usage: cz [-h] [--debug] [-n NAME]
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
...

Commitizen is a cli tool to generate conventional commits.
For more information about the topic go to https://conventionalcommits.org/

options:
-h, --help show this help message and exit
--debug use debug mode
-n NAME, --name NAME use the given commitizen (default:
cz_conventional_commits)

commands:
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
init init commitizen configuration
commit (c) create new commit
ls show available commitizens
example show commit example
info show information about the cz
schema show commit schema
bump bump semantic version based on the git log
changelog (ch) generate changelog (note that it will overwrite
existing file)
check validates that a commit message matches the commitizen
schema
version get the version of the installed commitizen or the
current project (default: installed commitizen)