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
7 min read

facebooklinkedintwitter
copy
7 Best Practices of Git Commit Messages

Introduction

Git commit messages are a vital part of the software development process. Well-crafted commit messages not only document the history of your project but also facilitate collaboration and maintainability. By following these git commit best practices, you can ensure clarity and efficiency in your development workflow, making it easier to track bugs, review code, and onboard new developers. This article will walk you through how to write git commit messages that are clear, concise, and informative.

Commit Sample

To structure your commits effectively, consider the following format:

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.

Understanding the commit messages convention is crucial for maintaining consistency across your project's history. Using the appropriate type ensures that your commits are categorized correctly, aiding in future code reviews and maintenance.

Optional Content

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

Subject

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

  • Use the 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.

7 Rules for Effective Commit Messages

To write impactful commit messages, adhere to these git commit message best practices:

  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".

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

Good Commit Examples

The commit message indicates a change has been made to the filtration logic to address and fix an issue related to filtering. The commit is categorized as a fix, resolving a bug or issue in the filter logic.

The commit message indicates that a new feature related to authentication has been added to the codebase. Specifically, Google authentication functionality has been implemented or integrated into the system, categorized under feat, which introduces a new feature.

Expert Insight


A well-formed Git commit message is critical to the success of a project. It allows developers to understand the purpose of each change, track bugs more efficiently, and collaborate more effectively.

- Chris Beams

Referencing the official Git documentation also emphasizes the importance of clear, concise commit messages as a best practice within the Git community.

Conclusion

By following these commit message best practices, you can ensure that your commits are easily understood by your entire team, making collaboration smoother and project management more efficient. A consistent and well-structured commit history not only aids in tracking changes but also enhances the maintainability of your codebase.

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

FAQs

Q: What if my commit doesn't fit within 50 characters for the subject?
A: Keeping the subject within 50 characters ensures readability and clarity. If your message exceeds this limit, 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: While the body is not always necessary, providing additional explanation, context, and reasons for the change can be beneficial, especially for complex commits.

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).

Q: What is the best way to handle WIP (Work In Progress) commits?
A: For WIP commits, you can use a temporary type or subject like wip: initial setup. However, it's recommended to squash these commits or reword them before merging to maintain a clean commit history.

Q: How should I handle commits that revert previous changes?
A: Use the revert type in your commit message and reference the commit being reverted in the body. For example, revert: Revert "Add Google auth". This ensures clarity about what change was undone and why.

Additional Resources

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Content of this article

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