Writing Good Git Commit Messages
Conventions, formatting and best practices in commit messages are one of the small details that makes Git a great tool.
— By Yasin
Hey there 👋
git, it’s tempting to pick only some changed files or even some changed
lines within files to include in a commit and leave the other changes to be added in a future
git commit, right?
Usually, if we want to achieve this, we should have a clean history that makes it easier to find such relevant information. And to maintain a clean git history, we should use good, well-formatted commit messages and code commits that are as small as possible.
This is precisely why we should keep commits as small as possible. This allows their message to describe a single change rather than multiple changes that are unrelated but worked on at the same time. Small commits keep the history readable; it's easier to understand why the change was made when looking at a small commit in the future.
If we later find a small
git commit undesirable, we can quickly revert it. This is much more difficult if you wish
to revert a single change if they clump together many unrelated changes into a single git commit. Now let's go into
more detail about what makes a suitable format when writing commit messages.
The Good Stuff
We should structure the commit message like an email. Typically, the first line is treated as the subject and the rest as the body, and we use the commit subject as a summary of that commit when only a single line is present. And it should be 50 characters or fewer.
The remaining lines should be wrapped at 72 characters or fewer and separated from the subject by a single, blank line. The commit message body should describe what the commit does in as much detail as is helpful in the present tense.
If you’re using GitHub, GitLab, or any other related tool, the contents of commit messages can contain
Markdown! You may use it to add some formatting that looks good in ASCII or rendered, such as
lists bulleted with a
numbered with, for example,
Voilà, now you know how to compose a good commit message ✨. To reiterate visually, always remember this format: -
Finally, with the above practices, I do like to follow conventions to streamline my git history even further. I adhere to conventional commits in my usual workflows whenever I contribute to a repository. And oh! always remember to keep your commits bona fide!
Thanks a bunch for reading.