The Art of Writing Commits

A commit message basically says what the new code does to improve the project. By providing information about the development history, commit messages may help to understand for example how a bug you need to fix got there in the first place, or why a solution has be chosen over another.

Writing Commits

A commit must be written as if the first part of the sentence was If applied, this commit will .... For example, If applied, this commit will ...
Fix typo in introduction to user guide

  • Capitalize the subject line.
  • Do not end the subject line with a period.
  • Use the imperative mood in the subject line.
  • Limit the subject line to 50 characters.
  • Separate subject from body with a blank line.
  • Wrap the body at 72 characters.
  • Use the body to explain what and why.

The Conventional Commits specification

On top of commit messages, you may find in some projects, these following structural elements which help to communicate the intent:
docs: add setup guide section to README

  • build: edit build config, development tools or other changes, irrelevant to the user.
  • chore: edit grunt tasks. No production code change.
  • ci: edit CI configuration files and scripts.
  • docs: edit the documentation.
  • feat: a new feature for the user. (MINOR version)
  • fix: a bug fix for the user. (PATCH version)
  • perf: performance improvements. (PATCH version)
  • refactor: refactor production code, e.g. renaming a variable.
  • revert: undo changes to a repository's commit history.
  • style: edit format, missing semi-colons, white-space, etc.
  • test: add missing tests, refactoring tests; no production code change.

Commitlint

Commitlint is the ESLint for commit messages. This tool checks if any text aligns with a predefined commit format. You can configure these formats to your needs or adopt pre-built-in conventions, such as conventional commits. Commitlint will then review and validate the messages right before committing changes, pushing, or using any other Git hook. If you want these commit message validations to run automatically on every Git commit command, you can use Husky, a tool that enables you to set up Git hooks quickly. Git hooks are preconfigured custom scripts that get executed before an action is performed in Git. By default, all the installed hooks are available in the .git/hooks directory with each filename being a hook name.

Resources