Sitemap

Good Commit vs. Your Commit: How to Write a Perfect Git Commit Message

4 min readSep 30, 2023

--

Press enter or click to view image in full size

If you’re a developer, you’re probably familiar with Git, the widely-used version control system that allows you to track changes in your codebase and collaborate with others effectively. While Git itself is a powerful tool, one of the most critical aspects of using Git effectively is writing clear and concise commit messages.

A well-crafted, committed message can make a world of difference in the readability and maintainability of your codebase. It serves as a communication tool for you and your team, helping everyone understand why a particular change was made and what it entails. In this blog post, we’ll explore the difference between a good commit and a not-so-good one, and we’ll provide you with some practical tips on how to write a perfect Git commit message.

The Anatomy of a Git Commit Message

Before we dive into the dos and don’ts of writing Git commit messages, let’s break down the typical structure of a commit message:

  1. A One-line Summary: This should be a concise and descriptive one-liner that summarizes the essence of the commit. It should be around 50 characters or less.
  2. An Optional Body: A more detailed explanation of the changes you’ve made. This is not always necessary but can be invaluable for complex or non-obvious changes.
  3. Metadata: Additional information like issue or ticket numbers, co-authored-by lines, etc. These help in tracking and referencing relevant context.

Now that we’ve covered the basic structure, let’s explore the difference between a good commit and your run-of-the-mill commit.

Good Commit vs. Your Commit

Good Commit Message

A good commit message is like a good story — it’s clear, informative, and concise. Here’s an example:

feat: add user authentication

- Implement user registration and login functionality.
- Use bcrypt for password hashing.
- Store user data in a PostgreSQL database.
- Add authentication middleware for protected routes.

In this example:

  • The commit starts with a type (e.g., “feat” for feature) followed by a colon.
  • It provides a clear and concise one-liner summarizing the change.
  • The body explains the key details of the change, including what was implemented and how.

A good commit message enables you and your team to understand what the commit does without diving into the code itself. It’s also helpful for future reference and when performing tasks like code reviews and debugging.

Your Commit Message

On the flip side, an inadequate commit message can cause confusion and frustration. Here’s an example of a less-than-ideal commit message:

Changed some code

This message is vague and uninformative. It leaves everyone wondering what changes were made, why they were made, and how they might affect the codebase.

Tips for Writing a Perfect Git Commit Message

Now that you understand the importance of writing good commit messages, let’s go over some tips to help you craft perfect ones:

  1. Use Descriptive Titles: Start with a clear, concise title that summarizes the change. Use action verbs like “add,” “fix,” or “update” to indicate the type of change.
  2. Keep It Short: Limit the title to 50 characters or less. This ensures that it’s easy to scan through commit logs.
  3. Provide Context: If necessary, use the body of the commit message to explain why the change was made and any relevant details.
  4. Be Consistent: Establish a convention for your commit messages, such as using a specific format like “type: message.” This makes it easier for you and your team to parse commit logs.
  5. Reference Issues: If the commit addresses a specific issue or task, include a reference to it. Many platforms (e.g., GitHub, GitLab) can automatically link commits to issues when you use the correct syntax.
  6. Avoid Jargon and Abbreviations: Write your commit messages with the assumption that someone unfamiliar with the project will read them. Avoid technical jargon or ambiguous abbreviations.
  7. Proofread: Before committing, take a moment to review your message for clarity and correctness. Typos and errors can lead to misunderstandings.
  8. Consider the Future: Think about how helpful your commit message will be to someone who comes across it months or even years later. Your future self will thank you for the extra effort.

Conclusion

writing a perfect Git commit message is not just about following a set of rules; it’s about fostering effective communication within your development team and ensuring the maintainability of your codebase. Adhering to these best practices can significantly improve your commit messages and contribute to a more organized and productive development process. Happy coding!

--

--

Addhe Warman Putra
Addhe Warman Putra

Written by Addhe Warman Putra

My Nickname “Awan” taken from Name [A]ddhe [Wa]rma [n] it’s Cloud. Working in Cloud Environment GCP + AWS in Large Scale.