Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
7 Best Practices of Git Commit Messages
Development Tools

7 Best Practices of Git Commit Messages

Best Practices for Git Commit Messages

Oleh Subotin

by Oleh Subotin

Full Stack Developer

Sep, 2023
6 min read

facebooklinkedintwitter
copy
7 Best Practices of Git Commit Messages

When writing Git commit messages, adhering to guidelines is crucial for clarity and collaboration. In this article, we will discuss effective patterns and best practices that can significantly enhance the quality of your commit messages. Following these guidelines can provide valuable context and clarity for yourself and your team.

Commit Sample

To structure your commits effectively, consider the following format:

 

type

Choose one of the following types for your commit:

  • build: Related to build process changes
  • ci: Related to continuous integration setup changes
  • chore: About build process or auxiliary tool changes
  • docs: Limited to documentation changes
  • feat: Introducing new features
  • fix: Addressing bug fixes
  • perf: Enhancing code performance
  • refactor: Code changes without bug fixes or new features
  • revert: Reverting previous changes
  • style: Markup, formatting, or whitespace changes
  • test: Adding missing tests

 

optional content

Provide optional content within parentheses to provide additional context, for example:

 

subject

The subject should concisely describe the change. Follow these guidelines:

  • Use imperative form and present tense (e.g., "change" instead of "changed" or "changes").
  • Avoid ending the subject line with a period.

 

optional body

Similar to the subject, maintain imperative form and present tense. The body should explain the motivation behind the change and highlight any notable contrasts with previous behavior.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

7 Rules for Effective Commit Messages

To write impactful commit messages, follow these rules:

  1. Separate the subject from the body with a blank line.
  2. Limit the subject line to a maximum of 50 characters.
  3. Start the subject line with a lowercase letter and use the present tense.
  4. Avoid ending the subject line with a period.
  5. Use the imperative mood in the subject line.
  6. Wrap the body at a maximum of 72 characters per line.
  7. Utilize the body to explain the "what" and "why" of the changes rather than the "how."

Good Commit Examples

The commit message means that a change has been made to the filtration logic in the codebase to address and fix an issue related to filtering. The commit is categorized as a fix, which resolves a bug or issue. The filter is the specific area of code affected, as indicated in parentheses. The commit message suggests that the logic for filtration has been modified or altered in some way.

The commit message means that a new feature related to authentication has been added to the codebase. Specifically, it states that Google authentication functionality has been implemented or integrated into the system. The commit is categorized as a feat, indicating the introduction of a new feature. In this case, the feature is the ability to authenticate users using Google authentication.

By implementing these recommended patterns and best practices, you can ensure that your Git commit messages provide clear and concise information about the changes made. Well-structured commit messages promote collaboration, streamline code maintenance, and enhance project understanding for you and your team.

FAQs

Q: What if my commit doesn't match 50 characters for the subject?
A: Keeping the subject fit within 50 characters is recommended, ensuring readability and clearness. However, If your message exceeds 50 characters, focus on the most critical information and consider moving additional details to the optional body section.

Q: Can I use past tense in commit messages?
A: The present tense and imperative mood are recommended to make the message more actionable and align with Git commit message conventions.

Q: Should I always include the optional body in my commits?
A: The body is not necessary to include in the commit message. However, providing additional explanation, context, and reasons for committing is beneficial.

Q: How can I group related commits together?
A: To group related commits under a common category, use the same type and optional content but different subjects (e.g., feat(auth): Add Google authentication, feat(auth): Add Facebook authentication)

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

Additional Resources

¿Fue útil este artículo?

Compartir:

facebooklinkedintwitter
copy

¿Fue útil este artículo?

Compartir:

facebooklinkedintwitter
copy

Contenido de este artículo

We're sorry to hear that something went wrong. What happened?
some-alt