DEV Community

Cover image for Commit Patterns
Caio
Caio

Posted on

Commit Patterns

Commit Patterns are a set of rules and conventions that guide how commit messages are written in version control systems like Git. The primary goal is to make the project history clearer, more organized, and easier to understand for all team members and for the developer themselves in the future.

Why use Commit Patterns?

  • Transparency: Each commit becomes a small story, concisely describing what was changed and why.

  • Collaboration: It facilitates understanding of changes by other developers, speeding up code reviews and the integration of new features.

  • Traceability: It allows you to quickly locate specific changes, identify bugs, and understand the project's evolution.

  • Automation: Tools can be configured to automatically generate changelogs (lists of changes), version the software (semantic versioning), and even trigger CI/CD pipelines based on the types of commits.

  • Code Quality: Encourages smaller, more focused commits, which generally leads to cleaner and more modular code.

  • The Conventional Commits Standard

The most popular and widely adopted standard is Conventional Commits. It proposes a clear and standardized structure for commit messages, typically following this format:

<type>(<optional scope>): <description>

[optional body]

[optional footer(s)]
Enter fullscreen mode Exit fullscreen mode

Let’s break down each part:

Type (<type>)
This is the required prefix that indicates the nature of the change. Some of the most common types include:

  • feat: A new feature. Example: feat: adds login screen

  • fix: A bug fix. Example: fix: corrects validation error in signup form

  • docs: Changes to documentation. Example: docs: updates README with installation instructions

  • style: Changes that don’t affect the meaning of the code (formatting, spacing, etc.). Example: style: formats code structure

  • refactor: Refactoring code that doesn’t add a feature or fix a bug. Example: refactor: restructures authentication components

  • test: Adding or fixing tests. Example: test: adds unit tests for user service

  • chore: Changes to build process or auxiliary tools (e.g., updating dependencies, scripts). Example: chore: updates project dependencies

  • perf: Code changes that improve performance. Example: perf: optimizes product search query

  • build: Changes that affect the build system or external dependencies (e.g., npm, gulp). Example: build: updates webpack version

  • ci: Changes to CI configuration files. Example: ci: configures automatic deploy pipeline

  • revert: Reverts a previous commit. Example: revert: reverts layout change commit

2 Optional Scope (<optional scope>)

Provides more specific context for the change. Usually indicates which part of the project was affected.

Example:

feat(login): adds login functionality (scope is login)
fix(api): fixes error in user routes (scope is api)

Description (<description>)
A brief and concise description of the change, written in the imperative mood and present tense (as if giving an order). It is recommended to keep it under 70 characters.

Example:

feat: adds favorites button (and not "added a favorites button" or "adds a button for favorites").

Optional Body ([optional body])
A more detailed paragraph explaining the “why” and “how” of the change. It's helpful to provide additional context, justification, or technical details.

Optional Footer(s) ([optional footer(s)])
This may include information such as:

Breaking Changes: Used to indicate changes that might affect the operation of existing parts of the system or other modules. It is common to use BREAKING CHANGE: followed by a description.

References to issues or tasks: Links to project management tickets (e.g., Closes #123, Refs #456).

Example of a Standard Commit

feat(authentication): implements Google OAuth login system

This change adds support for user authentication via Google OAuth.
Users can now register and log in using their Google account,
streamlining the onboarding process.

`BREAKING CHANGE`: The `loginWithEmail` method has been deprecated
in favor of the new authentication structure.

Closes #42
Refs #18
Enter fullscreen mode Exit fullscreen mode

Tools to Assist

  • There are tools that can help enforce and validate commit patterns in your projects:

  • Commitlint: Helps validate commit messages when they are created, ensuring they follow the defined rules.

  • Commitizen: Provides an interactive command-line interface to help construct standardized commit messages, guiding the developer through the fields.

  • Husky: Allows setting up Git hooks, such as pre-commit, to run tasks like commit validation before they are finalized in the repository.

  • Adopting commit patterns is a practice that elevates the organization and efficiency of any software development project, especially in teams. Getting started may require a small adjustment in the workflow, but the long-term benefits in clarity and code maintenance are undeniable.

Bonus: Emojis

Commit Type

Commit Type Emojis
Initial commit 🎉 :tada:
Version tag 🔖 :bookmark:
New feature :sparkles:
To-do list (tasks) 🔜 :soon:
Bugfix 🐛 :bug:
Documentation 📚 :books:
Testes 🧪 :test_tube:
Adding a test :white_check_mark:
Passing test ✔️ :heavy_check_mark:
Accessibility :wheelchair:
Text (or content) 📝 :pencil:
Package.json em JS 📦 :package:
Work in progress 🚧 :construction:
Configuration files 🔧 :wrench:
Removing a dependency :heavy_minus_sign:
Adding a dependency :heavy_plus_sign:
Reverting changes 💥 :boom:
Code review changes 👌 :ok_hand:
Refactoring ♻️ :recycle:
Move/Rename 🚚 :truck:

Examples of use

git commit -m ":tada: Starting project"
git commit -m "heavy_plus_sign: build: Installing dependencies"
git commit -m ":sparkles: feat (header): Adding and positioning icons"
git commit -m "books: docs (readme): Creating project documentation"
Enter fullscreen mode Exit fullscreen mode

With the 4 commits above we have the following history:

Translation of the image above

docs(readme): Criando documentação do projeto RENATOADORNO committed 2 minutes ago
+: feat(header): Adicionando e posicionando os icones RENATOADORNO committed 8 minutes ago
+build: Instalando dependencias
RENATOADORNO committed 16 minutes ago
Iniciando projeto
RENATOADORNO committed 22 minutes ago
Enter fullscreen mode Exit fullscreen mode

Reference: conventionalcommits

Top comments (0)