DEV Community

Cover image for 🎯 The Art of Writing Awesome Commit Messages: Why It Matters and How to Nail It
Hadil Ben Abdallah
Hadil Ben Abdallah

Posted on

🎯 The Art of Writing Awesome Commit Messages: Why It Matters and How to Nail It

Hey there, code warrior! πŸ’»πŸ‘©β€πŸ’»

We all know the feeling. You're in the zone, slinging code like a boss, and you hit that beautiful moment when everything works. Time to commit! But wait, what do you type in that commit message box? πŸ€” "Fix stuff?" πŸ˜… Or maybe "asdfasdf?"

Let's put those bad habits to rest and talk about writing commit messages that will make your future self (and your teammates) πŸ’― grateful!

Why Bother with Commit Messages? πŸ€·β€β™€οΈ

Well, imagine reading a novel where all the chapter titles were "Chapter 1", "Chapter 2", and so on... Yikes! That's how your git history feels when your commit messages are vague, random, or worse, nonexistent.

A good commit message is like a breadcrumb trail πŸ₯– for your code, showing why things happened and what was done. It makes it easier to:

  • πŸ•΅οΈβ€β™‚οΈ Find the cause of bugs
  • 🧹 Clean up or refactor without losing your mind
  • 🀝 Collaborate effectively with other devs (they will love you for it!)

Good Commit Messages vs. Bad Ones 🀑 vs. πŸ†

The Bad Examples:

  • "fix" β€” Hmmm, what did you fix? A bug? The alignment of a button? The Wi-Fi?
  • "bug fix 2323" β€” Cool, but what bug? 😡
  • "asdfasdf" β€” We're calling the git police. 🚨

The Good Example:

  • "Fix the navbar not displaying correctly on mobile devices" β€” Perfect! It's clear, concise, and to the point. Your team will understand what changed instantly. πŸ™Œ

The Anatomy of a Great Commit Message 🧠

A good commit message is like a tweet: short, sweet, and informative! Here's a quick guide:

1. Start with an Action Verb

Keep it in the present tense (e.g., Add, Fix, Update).

  • βœ… Add new feature to handle payment processing
  • βœ… Fix broken link on homepage

2. Keep it Under 50 Characters

This is your subject line, so make it snappy!

  • ❌ Fix bug where user was unable to login when they clicked the login button, even though it was pretty obvious it should have worked...
  • βœ… Fix login button issue

3. Optionally Add a Description

For complex commits, add a line or two explaining the why behind the change.

Fix payment gateway integration.

The previous implementation was causing timeout issues on high-traffic days.
Enter fullscreen mode Exit fullscreen mode

4. Use Emojis to Lighten the Mood! πŸŽ‰

Emojis aren't just for Slack! You can throw them into commit messages to add clarity and fun.

  • ✨ Add animation to the hero section
  • πŸ› Fix typo in user registration flow
  • πŸ”§ Refactor code to improve readability

Examples to Inspire You πŸ’‘

  • "πŸ› Fix bug where users couldn't log in after a password reset"
  • "🎨 Update styles for better contrast in dark mode"
  • "πŸ”₯ Remove deprecated API calls"

The emojis are optional (but fun!), and the message itself tells the full story. You don't have to ask "What did this do?" It's right there!

When NOT to Use Emojis ⚠️

While emojis are cool, don't go overboard. If your commit history looks like a string of hieroglyphs, future you might not be so thrilled. Balance is key. Keep it readable! πŸ€“

Final Tips for Commit Message Success πŸ’ͺ

  • Commit often: Small, focused commits are better than giant monoliths of code
  • Be clear, not clever: Sure, witty commit messages are fun, but clarity always wins
  • Keep it relevant: Don't throw in unrelated changes. One commit = one purpose

Your Commit Message Template πŸ“

Here's a cheat sheet for you to follow:

# <Type of change>: Short summary (max 50 characters)
# 
# Optional body:
# - Explain the "why" behind the change
# - Include references to issues or tickets if relevant
# - Keep each line within 72 characters
Enter fullscreen mode Exit fullscreen mode

Example:

πŸ› Fix login bug on mobile devices

The login button was unresponsive on iOS devices due to incorrect 
event handling. This commit fixes the issue by using native click events.
Enter fullscreen mode Exit fullscreen mode

Wrap-Up 🎁

Remember, writing good commit messages is more than just a courtesy; it's an investment in your project's future. Your team will appreciate it, and you'll thank yourself when you revisit old code.

So, the next time you commit, take a deep breath 😀, follow these tips, and make that commit message count! πŸš€

Happy coding πŸ’»

Thanks for reading!

Made with πŸ’™ by Hadil Ben Abdallah.

GitHub LinkedIn CodePen Daily.dev X

Top comments (5)

Collapse
 
giulianobortolassi profile image
Giuliano Bortolassi

Nice advice! I am just a little bit skeptical about emojis. Most modern clients might be a le to proper render it ( if you have a emoji aware font installed).

But I am not sure how it can impact automated ci pipelines if these tools depend on commit messages, or even how it would render if you are using a terminal on a server without any "Fira Code" like font installed.

I personally avoid emotes in the commit messages.

But thanks for the article

Collapse
 
hadil profile image
Hadil Ben Abdallah

Thanks for your thoughtful comment! 😊 I totally get your skepticism about emojis in commit messages, and it's a valid point. Emojis can indeed be tricky in environments where rendering support isn't guaranteed, like in certain CI pipelines or terminals without emoji-aware fonts.

In those cases, I’d agree that it’s safer to stick to plain text, especially if there's a chance it might disrupt any automated tooling or lead to confusion in environments that don’t render them properly.

That being said, I think it really depends on the team and the project. For more casual or internal projects, emojis can add a fun and quick visual cue to commit messages. But for serious production code where consistency across different systems matters, avoiding emojis is definitely the safer bet. πŸ”

As @lancemdev said, you can get your emojis from gitmoji, so you don't be confused which emoji to choose in your message.

Thanks again for your feedback, and I’m glad you found the article helpful! πŸ‘

Collapse
 
polyglotdev profile image
Dom Hallan

It doesn't quite frankly, this has old man, "get off my lawn" vibes. I mean what CI/CD env are you messing with that doesn't have UTF-8? Not to mention emoji are not font dependent. Would you tell someone to not use Chinese characters because if the box can show unicode characters, emoji will be just fine.

Collapse
 
lancemdev profile image
Lance Munyao

Nice article.

You can get your git emojis from gitmoji btw

Collapse
 
hadil profile image
Hadil Ben Abdallah

Thank you so much. So glad that you like it 😍