4
7 Comments

Writing Good Git Commits

Let's talk about git commit messages & why they're important.

If you're like me, when you've been adding a feature or fixing a bug, you've come across some code that the you from 3 months ago wrote and you're scratching your head. "What the hell was I thinking?" you ask yourself.

You take a look at the git blame only to find the last time you touched that block of code, you left yourself the note "add invites". You don't know why you added them and the commit spanned across your entire stack so the diff is huge. "If only I would have taken the time then, to make this easier to understand now", you say before diving right into the code and start tearing it out.

Writing good commit messages can turn your codebase from the Rohonc Codex 🧐 into an Indiana Jones-level treasure map 🤩. Here are a few suggestions to write a better git commit history and to help your future self:

  1. Add structure

Make it easy to scan to get the scope of the change in one line. The angular commit format is great for capturing a lot of detail in a compact format:

✅ “feat(invites): Add invites to athletes page”

  1. Lead with Why

Why defines the problem being solved by the commit.

✅ “We need to give race organizers a way to quickly invite more athletes to their event and athletes a way to easily join events they've been invited to.”

  1. Follow with What

What describes, in terms the codebase uses, how the problem was solved by the commit

✅ “Added a migration to add the invite_code field to a race. Added a inviteCode field to the Race GraphQL type. Added some behavior to generate an invite code for a race when it is created. Added a button & popover to the race athletes page that allows an athlete to copy the invite link and send it to their friends.”

  1. What dos & donts

Don't write about what the commit’s diff clearly shows (files changed, code changes, etc)

🚫 “We updated race-schema.js & RaceAthletesPage.ts and updated the build-router function…”

Do write in terms of the domain

✅ “We added a inviteCode field to Race & to the query on the race page"

  1. Changes are causal graphs

Writing a good “what” is like writing a story. Changes to your code are causal graphs and so are stories. Instead of our previous "What" example, let's try describing the change as it unfolded:

🏆 "In order to support creating a unique link as an invite link for a race, we need to add the notion of an invite code. To associate an invite code with a race, we added a field to the races table in the database. After that, we were able to begin generating one upon creation of a race. Then..."

📝 The final product:

feat(invites): Add invites to athletes page

Why:
We need to give race organizers a way to quickly invite more athletes to their event and athletes a way to easily join events they've been invited to.

What:
In order to support creating a unique link as an invite link for a race, we need to add the notion of an invite code. To associate an invite code with a race, we added a field to the races table in the database. After that, we were able to begin generating one upon creation of a race. Then...

There you have it. You can quickly understand the scope of the change, why you made it and the story behind what was changed to support the invites feature.

I hope you found some of this advice helpful. Happy git committing!

on August 14, 2020
  1. 2

    If you are working with a team or using an issue tracker, you can also include links to code reviews and the relevant issue.

    1. 1

      Great suggestion! This can be a huge help in giving a fuller context for the commit for sure.

  2. 2

    Clear git commit messages (and atomic commits) are key! The same goes for pull requests! One more thing that has saved me immense time as I revisit commits is adding a shortened (doesn't have to be) link to the resource related to the commit: e.g. StackOverflow post, some 3rd party integration documentation, etc.

    1. 2

      So true, I'll be posting something around atomic, deployable commits this week. Especially when a bug may be caused by a library, linking to the issue can be a great way to build some context for the workaround you're having to implement.

      1. 1

        Great, looking forward to your next post then, keep up the great content!

  3. 2

    This comment was deleted 6 years ago

    1. 2

      For sure, that's another angle to optimize for. You have two stakeholders here:

      1. Maintainers
      2. Operators

      For me, operators (whoever's responsible for managing the codebase running in an environment) are targeted by that first line (the angular commit convention). Maintainers, if they're reading the commit message for whatever reason, are likely looking for more detail and so the "why/what" areas are more for them.

      I'll be posting a followup about how to organize commits as well, which will touch on this a bit more.