You writing "fixed" is hurting your whole team
If you haven’t read Chris Beams’ infamous article about writing git commit messages, you should! That and other similar posts are where I pull many of my thoughts on the topic from. XKCD also has a fun comic on this topic.
The Beams article makes a lot of fantastic points and you should absolutely read it if you haven’t already. He lays out seven rules, and I would add two more:
- Separate subject from body with a blank line
- Limit the subject line to 50 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the
in the subject lineimperative mood - Wrap the body at 72 characters
- Use the body to explain what and why vs. how
- Use Conventional Commit prefixes at the beginning of the subject
- Do not use the word “and”, the word “also”, or a comma in the subject
In this post, instead of repeating what more articulate authors than I have already said, I’m going to talk about the motivations behind the aforementioned rules, explain my additions in more depth, and talk about the most common mistakes that I see developers make when writing commit messages.
MotivationSection titled: Motivation
The motivation behind each of the rules that involves the subject line can be summed up in a single word,
One could perhaps restate those rules as:
- Get to the point already
- Don’t waste one of your valuable 50 characters on useless punctuation
- Don’t waste characters with pointless suffixes like “ing” or “ed”
Rule #8Section titled: Rule #8
- Use Conventional Commit prefixes at the beginning of the subject
Conventional commits, in their own words are:
I have found that this convention makes commits clearer, while also encouraging them to be atomic.
The first place that I came across this syntax was in the Angular repo, but a number of other well-known projects have also adopted the convention. For example:
Rule #9Section titled: Rule #9
- Do not use the word “and”, the word “also”, or a comma in the subject
One of the biggest reasons that I think Conventional Commits are so important is that they force you (if you’re not lying to yourself your team in your commit message) to make each commit atomic.
Having “and”, “also”, or a comma in your commit message means that your commit is not atomic, because it does at least two separate things.
The solution here isn’t to omit half of the message, but instead to separate that one commit into two or more individual commits, each with good commit messages of their own.
Common MistakesSection titled: Common Mistakes
Justifications in the subjectSection titled: Justifications in the subject
The subject should be used to explain in the fewest number of characters possible what you did, never why you did it. Why should always be put in the body of the commit.
If I want to know why you did something, I’m already invested in it and I’m going to read the entirety of your commit message. No one needs to know why you did something at a glance. They need to know what you did at a glance.
Also, by its very nature, why will usually be lengthier than what, and you’re not limited by the number of characters you can use in the body of the commit.
Never writing commit bodiesSection titled: Never writing commit bodies
Many developers simply never write more in a commit than fits in the subject. This is unfortunate because complex changes, or even simple changes that were tricky to make really deserve more explanation than that.
- When you fixed a bug, what lead you to this solution?
- Did you try other things first that didn’t work?
- Did you find the eventual solution somewhere online?
- Drop the link so that your teammates or future you can reference that later!
- Was this feature particularly complicated? What problems did you encounter while writing it?
- Is there anything that you know right now that might come in handy if you or a teammate ever needs to touch this code again?
I estimate that one out of every four commits that I write has a body to explain something. So I am certain that you will also create the occasional feature that your teammates would benefit from you explaining further.
Repetitive ticket numbersSection titled: Repetitive ticket numbers
Ticket numbers are really useful to have in your commit structure, but putting them in every commit wastes your valuable 50 characters and gets super repetitive.
Similar to the why piece of the commit message, if I want to know what ticket number a particular commit was involved with, I have no problem taking a few moments to quickly find that information, rather than having that information thrown in my face over, and over, and over again.
Instead of including the ticket number in every commit, try only including it in the commit created when you merge your Pull Request (whether that be a merge commit or a squash commit). If you really must include the ticket number in every commit, try putting it in the body of the commit instead.
One or two word commit messagesSection titled: One or two word commit messages
Looking at the above list of commits, how many of them can you tell what that commit did? How many of them can you even make a guess?
Overly brief commit messages fail the one and only task of a commit message - to tell you what the commit did!
Having something like WIP
commit makes sense as you’re working. Committing early and often make sure you never lose progress you’ve made. But before you ever make a Pull Request for those commits, you should have gone back and cleaned them up. Often that means squashing several WIP commits together into a single meaningful commit.