Git Commit Best Practices: Examples of Good and Bad Commit Messages

Abdul Wahid Muhaemin
7 min readJul 26, 2024

--

Git commit is an essential feature of Git that allows us to document and save every change we make in the code. Although often considered a technical detail, good commit messages significantly impact the software development process, especially in projects involving multiple contributors.

Good commit messages not only help in maintaining an organized change history, but also make it easier for other teams to understand the intent of any changes made. This is especially important in team collaboration, debugging, and feature tracking. Conversely, poor commit messaging can make the development process chaotic, make it difficult to track issues and slow down project progress.

In this article, we will discuss best practices for writing effective commit messages and provide real-life examples of good and bad commit messages. By following these guidelines, we will be able to improve the quality of project history, facilitate team collaboration, and increase the overall efficiency of software development.

Why Good Commits Matter

Good commits are more than just documentation; they are the foundation of an efficient and collaborative software development process. Here are several reasons why good commit practices are essential:

1. Enhances Team Collaboration
Clear and informative commit messages help other team members understand the changes made without having to dive deeply into the code. This facilitates communication and coordination within the team, ensuring everyone is on the same page regarding project progress.

2. Facilitates Debugging and Maintenance
A well-structured commit history makes debugging easier. When issues arise, we can quickly trace back to the changes that might have introduced bugs and identify the relevant commits to address the issue. It also simplifies long-term maintenance by providing context on the changes made.

3. Supports Revert and Rollback
In software development, there are times when we need to undo or roll back unwanted changes. Clear and detailed commits allow us to quickly locate the commit that needs to be reverted, minimizing negative impacts on the project.

4. Maintains Project Documentation
Commit messages serve as evolving documentation for the project. By including appropriate descriptions in commit messages, we ensure that the project history can be understood by anyone reviewing it in the future, including new team members or developers not directly involved in the project.

5. Improves Code Quality
Well-structured commits often reflect good thought processes and attention to detail in code development. This encourages disciplined coding practices and helps maintain high code quality standards in the project.

By understanding and implementing good commit practices, we can ensure that software development is more organized, transparent, and efficient, benefiting both ourselves and our team.

Best Practices for Commit Messages

Crafting effective commit messages is crucial for maintaining a clean and organized project history. Here are some best practices to follow when writing commit messages:

  1. Write Clear and Informative Messages
  • Example: Fix login issue on homepage
  • Commit messages should be concise yet descriptive enough to explain the change without requiring further context. This helps other team members quickly understand the purpose of the commit.

2. Use the Imperative Mood

  • Example: Add user profile editing feature
  • Write commit messages in the imperative mood (e.g., “Add,” “Fix,” “Remove”). This style is consistent with the conventions of most version control systems and aligns with the way commits are typically applied.

3. Keep the Subject Line Short and Specific

  • Example: Refactor data processing module
  • The subject line should be a single line of text, ideally under 50 characters, that summarizes the change. This helps in quickly scanning the commit history.

4. Include a Detailed Description When Necessary

Example:

git commit -m "Fix login issue on homepage

- Addressed a bug where users could not log in due to a missing authentication token.
- Updated login logic to handle token generation and validation more robustly.
- Added test cases to ensure proper token handling."
  • If the change requires additional explanation, use a blank line to separate the subject line from the body of the message. The body should provide context, reasons for the change, and any other relevant details.

5. Reference Issues or Tasks

  • Example:
git commit -m "Fix issue #123: Correct header layout on mobile"
  • Link commits to relevant issues, tasks, or tickets to provide context about the change. This practice helps in tracking related work and understanding the reason behind changes.

6. Avoid Generic Messages

  • Example of Bad Message:
git commit -m "Update code"
  • Generic messages like “Update code” or “Fix bugs” are unhelpful as they lack detail about the changes. Always strive to be specific and descriptive.

By following these best practices, we ensure that our commit history remains clear and useful for current and future collaborators. This approach not only improves the overall quality of the codebase but also enhances collaboration and project management.

Examples of Good Commits

Effective commit messages provide clarity and context for the changes made. Here are some examples of good commit messages that follow best practices, including naming conventions:

fix(auth): resolve bug in user authentication process

  • Context: This commit fixes a specific issue in the authentication module. Using a naming convention like fix(auth) helps categorize the type of change and the affected module, making it easier to understand and locate the change.

feat(profile): add user profile editing feature

  • Context: This commit introduces a new feature related to user profiles. The feat(profile) prefix indicates that this change adds a new feature and specifies the module affected.

refactor(data): improve performance in data processing module

  • Context: This commit refactors the code to enhance performance. The refactor(data) prefix categorizes the change as a refactor and specifies the module that has been improved.

docs(api): update documentation for API endpoints

  • Context: This commit updates the documentation for API endpoints. The docs(api) prefix indicates that the change is related to documentation and specifies the affected area.

remove(deprecated): eliminate old methods from user service

  • Context: This commit removes deprecated methods from the user service. The remove(deprecated) prefix categorizes the change as removal and specifies that outdated code is being eliminated.

fix(ui): correct layout issues on mobile devices

  • Context: This commit addresses layout issues specifically on mobile devices. The fix(ui) prefix categorizes the change as a fix and specifies that it affects the user interface.

Naming Conventions Explained

  • fix: Used for commits that fix bugs or issues.
  • feat: Used for commits that introduce new features.
  • refactor: Used for commits that involve code refactoring without changing functionality.
  • docs: Used for commits related to documentation updates.
  • remove: Used for commits that remove features or code.
  • ui: Specifies changes related to the user interface.

Using these naming conventions helps maintain a structured commit history, making it easier for team members to understand the type and scope of changes at a glance.

Examples of Bad Commits

Bad commit messages can create confusion and hinder project management. Here are some examples of poor commit messages and why they fall short:

update

  • Issue: This message is too vague and does not provide any information about what was updated or why. It lacks context and specificity.

fix

  • Issue: While this message indicates that something was fixed, it does not specify what was fixed or the nature of the problem. It’s too ambiguous to be useful.

changes

  • Issue: This message does not describe what changes were made. It fails to provide any detail about the content of the commit, making it difficult to understand the impact.

stuff

  • Issue: This message is informal and non-descriptive. It does not communicate what was changed or the purpose of the commit, which can lead to confusion.

final fix

  • Issue: Using terms like “final” can be misleading and imply that no further changes will be made. Additionally, it does not describe the nature of the fix or the issue addressed.

12345

  • Issue: Using numbers or arbitrary strings as commit messages is not informative. It does not convey any meaningful information about the changes made.

Why These Messages Are Problematic

  • Lack of Context: These messages do not provide enough information to understand the purpose or impact of the changes.
  • Ambiguity: Vague messages make it difficult to determine what has been modified or why the change was made.
  • Difficulty in Debugging: Poor commit messages can make debugging more challenging, as it’s harder to trace the history of changes and understand their context.
  • Hinders Collaboration: Team members may struggle to understand the changes or their implications, which can affect productivity and collaboration.

By avoiding these bad practices and adhering to the best practices discussed earlier, we can ensure that our commit history remains clear, useful, and conducive to effective collaboration.

Conclusion

Effective commit messages are crucial for maintaining an organized and efficient development workflow. By following best practices and avoiding common pitfalls, we can enhance our project management, facilitate better collaboration, and improve the overall quality of our codebase.

Key Takeaways

  1. Clarity and Specificity: Always aim to write commit messages that are clear, specific, and informative. This helps in understanding the purpose and impact of each change.
  2. Consistent Naming Conventions: Adopting a consistent naming convention for commit messages helps in categorizing and organizing changes effectively. It also makes the commit history easier to navigate and understand.
  3. Avoiding Vague Messages: Steer clear of vague or non-descriptive commit messages. Instead, provide enough detail to convey the nature of the change and its context.
  4. Regular Reviews: Periodically review commit messages to ensure they adhere to best practices. This can help maintain a clean and useful commit history.

By implementing these practices, we can contribute to a more manageable, transparent, and efficient development process. Well-crafted commit messages not only benefit current collaborators but also future contributors who will look back at the project’s history.

Remember, good commit messages are a sign of a well-maintained codebase and an effective team. Let’s make the effort to write meaningful messages and uphold the standards that contribute to successful project development.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response