Write Better GitHub Commit Messages: A Step-by-Step Approach

Rizwan
5 min readJan 20, 2025

Committing code to GitHub is more than just saving changes; it’s about maintaining a clean and understandable history of a project not just pushing the changes to GitHub branches. Well-structured commits help teams collaborate effectively and make it easier to debug, review, and understand code. But how do you ensure that your commits are meaningful and your messages convey the right information about the commit you made? Let’s dive into some best practices and tips for mastering GitHub commits which will help you in your future projects and engage with team efficiently.

Why Are Good Commits Important?

Good commits are the backbone of any collaborative development project. Here’s why they matter:

  • Better Collaboration: Clear commits make it easier for team members to understand changes and avoid redundant work.
  • Easier Debugging: When something goes wrong, atomic and descriptive commits make it simpler to pinpoint issues.
  • Improved Code History: A clean commit history provides a reliable timeline of what happened and why.
  • Professionalism: Whether you’re working solo or contributing to open-source projects, good commits reflect professionalism and attention to detail.

Best Practices for Committing Code

1. Commit Frequently

Don’t wait too long before committing your work. Small, incremental commits ensure that changes are manageable and easier to review. Frequent commits also reduce the chances of merge conflicts.

Usually one task or feature require a commit.

2. Make Atomic Commits

Each commit should represent a single, complete change. For example, if you’re fixing a bug and adding a new feature, commit them separately. This makes it easier to trace specific changes later when there are multiple commits in the branch.

3. Review Changes Before Committing

Use git diff or your Git client’s visual diff tool to review changes before committing. This step helps you avoid committing unintended changes, such as debug code or sensitive information. This step can also be done in in Visual studio/ Visual studio code or via GitHub Desktop User interface.

4. Write Meaningful Commit Messages

A good commit message tells the story of the change. Include both a concise summary and, if necessary, a detailed explanation of what and why the change was made.

5. Follow Project Guidelines

If you’re contributing to a team or open-source project, adhere to their commit message conventions and branching strategy.

Mostly open-source repositories has their own document to make changes in the repo and the proper format of commit messages with label in it.

6. Use .gitignore

Make sure you’ve configured a .gitignore file to exclude unnecessary or sensitive files, such as logs, temporary files, or build artifacts. This process is very important otherwise your personal or payment information can be used by unauthorized person. Make sure you have saved your keys and connection string in the secret files and that file path should be in the .gitignore file.

7. Test Before Committing

Always test your code before committing it. This ensures that your changes don’t break functionality or introduce new bugs or run possible test if it’s already written. Usually manual testing can be performed for the functionality.

8. Avoid Committing Debugging Code

Remove any console.log statements, debug prints, or commented-out code before committing. Keep the repository clean and professional. and also remove the comments you have made for your personal understanding to get the logs or make the hint in the code.

9. Commit to the Right Branch

Make sure you’re working on the correct branch for your feature or bug fix. Avoid committing directly to the main or master branch unless explicitly required.

How to Format Commit Messages

The Ideal Structure

A well-formatted commit message generally follows this structure:

<type>(optional scope): <short description>

[optional body]

[optional footer]

Let’s break it down:

1. Commit Types

Start your commit message with a type that describes the nature of the change. Some common types include:

  • feat: A new feature (e.g., feat(auth): add user login functionality)
  • fix: A bug fix (e.g., fix(cart): resolve duplicate item issue)
  • docs: Documentation updates (e.g., docs(readme): update setup instructions)
  • style: Code style changes that don’t affect functionality (e.g., style: reformat code with Prettier)
  • refactor: Code changes that neither fix bugs nor add features (e.g., refactor(api): simplify token validation logic)
  • test: Adding or updating tests (e.g., test: add unit tests for user service)
  • chore: Maintenance tasks like dependency updates (e.g., chore: upgrade dependencies)

2. Short Description

The first line should be a concise summary of the change, ideally no more than 50 characters. Use the imperative mood (e.g., “Add”, “Fix”, “Update”).

3. Optional Body

If the change is complex, add a body to explain:

  • What was changed
  • Why it was changed
  • How the problem was solved

Wrap lines in the body at 72 characters for readability.

4. Optional Footer

Include additional information in the footer, such as references to issues or breaking changes.

  • Breaking Changes: BREAKING CHANGE: Renamed authToken to accessToken.
  • Issue Reference: Closes #1234

Examples of Good Commit Messages

Simple Commit:

single line commit to keep the message simple or if there is nothing much information required for the commit.

fix(api): resolve null pointer error in user service

Detailed Commit:

Specially for the hot-fix or for the big change, where a scenario based issue as been fixed or expression the flow of the feature.

feat(payment): add support for multiple currencies

This feature allows users to process payments in USD, EUR, and GBP.
The conversion rates are fetched from a third-party API in real time.

Closes #456.

Conclusion

Good GitHub commits are an essential skill for any developer. By following these best practices and formatting your commit messages effectively, you can maintain a clean and professional codebase. Whether you’re working solo, with a team, or contributing to open-source projects, mastering the art of committing code will improve collaboration, save time, and elevate the quality of your work.

Make habit to write the clean commit for your personal projects as well, that will boost your commit writing technique even when you are in hurry you will be able to write near to best possible commit message.

Don’t try to think much while adding commit , you could add a generic message to your changes and you can also change it later whenever necessary using GitHub amends.

Follow me for more article like this. and ask anything you need or share your commit message techniques. I would love to hear that.

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

Rizwan
Rizwan

Written by Rizwan

Senior Software Engineer | Mentor | Tech Enthusiast | Passionate about building scalable solutions, exploring the latest technologies.

No responses yet

Write a response