Good Git commit messages
Hey there 👋
When using 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 keeps 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 try to revert a single change if they all 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
using **bold**
, _italics_
, ~~strikethrough~~
, monospace
or lists
bulleted with a *
, -
, or
numbered with, for example, 1.
*.
I was inspired by Tim Pope's article A Note About Git Commit Messages in his blog, and you should definitely check it out if you want to understand the reasoning behind these rules.
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.
Well, now what?
You can navigate to more writings from here. Connect with me on LinkedIn for a chat.
2023
December
October
August
June
May
March
January