Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

How to write Git Commit Messages

When developers first step into the world of Git, they often encounter a certain discomfort, especially when it comes to crafting meaningful commit messages. The Git commit message, which encapsulates the changes made and why they were made, is a crucial part of version control. Developing good committing habits early in your career is essential. In this guide, we’ll explore how to improve your Git commit messages and why it matters.

The Importance of Quality Commit Messages

Imagine this scenario: you open a repository, run git log, and find a list of cryptic commit messages like “Fix style” or “Update code.” Months later, you revisit the project and have no idea what these messages mean. Sound familiar? Well, this is a common issue with poorly written commit messages. Quality commit messages provide essential context and save you and your teammates time when troubleshooting or understanding past changes.

The Anatomy of a Commit Message

A Git commit message has two main parts: the subject line and the body.

  • Subject Line: This should be concise, not exceeding 50 characters. It should start with a capital letter and use the imperative mood (e.g., “Add,” “Fix,” “Update”). The subject line provides a high-level overview of the change.
  • Body: The body of the message is where you can explain in more detail. It should be wrapped at 72 characters and answer the questions of “why” and “what.”

Steps to Writing Better Commit Messages

Let’s dive into practical steps to improve your commit messages:

1. Capitalization and Punctuation

Always start the subject line with a capital letter and don’t end it with punctuation. Conventional Commits, a popular convention, encourages using all lowercase.

2. Mood Matters

Use the imperative mood in the subject line. This creates a tone of giving an order or request. For example, “Fix the dark mode toggle state” is more appropriate than “Fixed the dark mode toggle state.”

3. Specify the Type of Commit

It’s beneficial to categorize your commits. Use a consistent set of words to describe your changes. For example, “Bugfix,” “Update,” “Refactor,” or “Bump.” This helps in classifying and searching for specific types of changes.

4. Watch the Length

Keep the subject line short, ideally under 50 characters, and restrict the body to 72 characters per line. This ensures messages are concise and readable.

5. Content Clarity

Be direct and avoid filler words and phrases. Think like a journalist, aiming to answer the “what” and “why” questions. Pretend that the reader has no prior knowledge of the change and needs a clear explanation.

Conventional Commits

Conventional Commits is a widely adopted format that provides structured commit messages. It uses the format: <type>[optional scope]: <description>. The type can include values like “feat” (new feature), “fix” (bug fix), “chore” (maintenance), and more. The optional scope specifies the part of the codebase affected, and the description provides a concise summary.

This convention enhances consistency in commit messages and can be paired with tools like Commitizen to enforce standards.

Commit Message Comparisons

Let’s compare some examples to see the difference quality commit messages make:

Good:

  • feat: Improve performance with lazy load implementation for images
  • chore: Update npm dependency to the latest version
  • Fix bug preventing users from submitting the subscribe form
  • Update incorrect client phone number within the footer body per client request

Bad:

  • Fixed bug on the landing page
  • Changed style
  • Oops
  • I think I fixed it this time?
  • Empty commit messages

By following these guidelines, your commit messages can become valuable documentation, saving you and your team time and frustration. Whether you’re working solo or in a collaborative development environment, investing a little extra effort in crafting clear and meaningful commit messages pays off in the long run.

Related Posts

Laravel Throttle Middleware: How to Increase API Rate Limit Safely and for 429 Too Many Requests

If you’re working with Laravel APIs, you might have encountered this default throttle setting: This line lives in your app/Http/Kernel.php file and controls how many requests a…

Fixing MySQL Error: Incorrect Definition of mysql.column_stats Table

The Problem While working on your MySQL server, you might come across this error in your error log: This error usually shows up after an upgrade or…

Fixing Laravel Migration Error: “Unknown Collation: utf8mb4_0900_ai_ci”

While working with Laravel and MySQL, you might run into an error during migrations like this one: Why This Happens The collation utf8mb4_0900_ai_ci is introduced in MySQL…

Why Dental Surgery Is Good and Important

Dental health plays a vital role in our overall well-being, yet it’s often overlooked until problems become serious. Dental surgery is a powerful solution that not only…

How to Get Cosmetic Surgery Covered by Insurance

Cosmetic surgery has become increasingly popular for individuals seeking to enhance their appearance or correct certain physical issues. While many people assume that cosmetic procedures are always…

Real-Time Memory Monitoring in Linux with free -m and watch

When your Linux system starts slowing down, the first suspect is usually memory. Is RAM maxing out? Is swap being used? Is some process eating up everything?…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x