Best Practices for Commits and Commit Messages

Version control is an essential part of modern software development, and Git, along with GitHub, is one of the most popular tools for version management. A fundamental part of using Git effectively is the ability to create clear and informative commits. Below are best practices for making commits and writing commit messages that help maintain a clear and useful project history.

1. Make Small, Focused Commits

Commits must be atomic, meaning that each commit must represent a single logical unit of change. This makes it easier to review code, test isolated changes, and find bugs. If you are working on multiple tasks at the same time, split them into separate commits.

2. Write Clear and Descriptive Commit Messages

Each commit must have a message that clearly describes what was changed and why. A good commit message allows other developers, and even you in the future, to quickly understand the purpose of the change without having to analyze the code in detail.

3. Use the Present Imperative

Git itself uses the present imperative in its automatically generated messages - for example, "Merge branch 'x'". Follow this style when writing your own commit messages. Start with a verb in the present imperative, such as "Add", "Update", "Remove", etc. This creates a series of instructions that describe what the commit will accomplish if applied to the code.

4. First Line as a Summary

The first line of your commit message should be a brief description of the change, limited to 50 characters if possible. This acts as a title for the commit. If necessary, leave a blank line after the summary and continue with a more detailed description in subsequent paragraphs.

5. Use Message Body When Necessary

For complex changes, one line will not be enough to describe what was done and why. In these cases, after the summary, add a detailed body to the commit message. Explain the context of the change and compare it to the code's previous behavior.

6. Avoid Generic Messages

Messages like "fix bug" or "improve code" do not provide useful information. Be specific about what bug was fixed or how the code was improved.

7. Reference Issues and Pull Requests

If your commit is related to an issue or pull request on GitHub, include the reference in the commit message. This creates an automatic link between the commit and the issue/pull request, making it easier to track specific changes.

8. Avoid Jargon and Obscure Technical Terms

Although your audience will primarily be other developers, avoid using jargon or technical terms that are not widely understood. This ensures that commit messages are accessible to all team members, regardless of their technical level.

9. Review Your Commits Before Publishing Them

Before pushing your commits to the remote repository, review them. Make sure each commit is atomic and that messages are clear and informative. This may involve rearranging, splitting, or combining commits.

10. Consistency in Commit Messages

Maintain a consistent style in your commit messages. If you're working in a team, it's helpful to have a style guide that everyone follows. This may include the structure of messages, the use of tags (such as [FEATURE], [FIX], [REFACTOR]), and other standards the team has agreed to follow.

11. Use Git Tools and Hooks

Git provides hooks that can be used to enforce commit message standards or to perform checks before commits are accepted. Use these tools to help maintain the quality of your commit messages.

12. Educate your Team

If you're leading a team or working on a collaborative project, educate your colleagues about the importance of good commits and commit messages. A well-managed repository is a team effort.

Conclusion

Commits and commit messages are more than just a technical record; they are the story of your project. Following these best practices will help make this story clear, useful, and accessible to everyone who works with the code. Additionally, a well-maintained commit history can be a valuable tool for code review, debugging, and collaboration.

Now answer the exercise about the content:

Which of the following statements best reflects one of the best practices for commits and commit messages as per the text provided?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Code review and code ownership

Next page of the Free Ebook:

55Code review and code ownership

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text