11 tips for writing a good Git commit message

11 tips for writing a good Git commit message
AmyJune
Wed, 12/28/2022 – 03:00

Lately, I have been paying closer attention to the changelogs I get from products and services when updates are needed. Here are some examples:

  • Fixed some bugs. 
  • Made some accessibility improvements. 
  • We’ve made improvements and fixed bugs for a smoother ride.

When I think about some of the first commit messages I made as a junior developer I have to hang my head in dismay:

  • Pointed and clicked around a bit and now things seem to work.
  • Did what programmer X told me to do and now the banner is blue.

This can be frustrating! I asked our community of contributors the following questions:

  • What makes a good Git commit message?
  • What makes a bad one?
  • What rules do you think a project should have around what a commit message contains?

Here are their answers:

Great writing is key

As with anything you write, you should think about who is going to read it. Then adapt the amount and depth of information accordingly.

Improving your natural language and writing skills is essential for a healthy career in software development. It’s not just code that counts.

Camilla Conte

Be descriptive and don’t assume

I spend a lot of my time collaborating in the OpenStack community, and its code reviewers have some fairly exacting standards compared to what I see from other projects “in the wild.”

I’ll often spend far longer composing a solid commit message than I do writing the actual code implementation or fix. Sometimes commit messages can end up many times longer than the diffs they’re explaining.

To summarize some of the contributor guidance:

  • Describe why a change is being made, not just what is changing
  • The first commit line is the most important (like the subject line of an email)
  • Don’t assume reviewers understand the original problem you’re fixing
  • Don’t assume the reviewer has access to external web services or the site (summarize defect reports and other relevant discussions)
  • Don’t assume the code is self-evident and self-documenting (though there is no need to repeat points you also make in your code comments)
  • Don’t include information only relevant to earlier revisions of the change (we expect contributors to squash revisions together and edit their commit messages accordingly).

There’s a brief section on the topic in the OpenStack Contributors Guide: https://docs.openstack.org/contributors/common/git.html#commit-messages

Jeremy Stanley

Your future self will thank you

I cannot agree more with Jeremy. +1000

Jeremy said, “describe why a change is being made, not just what’s changing.”

Imagine you’re someone else, in a faraway future, trying to work out this commit.

Put yourself in other people’s shoes, as the old saying goes.

Leigh Morresi

Use the bug ID

I recommend adding the bug ID at the start of the commit message so that it’s easier to track the commits at a later stage using the grep command.

For example:

$ git commit -m "BZ#19xxxxx 

To come up with thoughtful commits, consider the following:

  • Why have I made these changes?
  • What effect have my changes made?
  • Why was the change needed?
  • What are the changes in reference to?

Agil Antony

Tell the whole story

I like to imagine there is a hidden prefix to every commit message that reads “By applying this.”

A good commit message includes exactly what will happen and why. It is insufficient to merely have the work ticket reference because that decentralizes the information; Git is decentralized. As a software developer, I want to know why the proposed changes are being considered. What specific problem is being addressed? What alternate solutions were considered (and discarded)? What unexpected things were discovered during the creation of the changeset that influenced the current content?

There’s no prize for shortest commit message. Your future self and future colleagues will appreciate you going into depth to explain the problem and why this changeset is the answer. Harness those cooking blogs where there’s a five-paragraph life story. Here, however, make the problem the subject of the life story.

Lisa Seelye

But don’t be overly verbose

A good git commit message contains information about what was done, and nothing else. For instance, if you needed to update the .gitignore, just say “updated .gitignore.” Folks can dive into the commit itself for more details. It doesn’t need to be verbose.

A bad commit message is something like, “oh crap” or “try this”. Granted, I’ve been guilty of this, but it doesn’t help anyone if they need to look at commits at a glance.

Rules are very subjective. They can differ from lead to lead and team to team. But at the very least, give some contextual information about the commit. Especially if it’s a large one. No one has time to skim through 1000+ files with a heavy change history.

Miriam Goldman

Use present tense

I like project manager-styled commit messages written in present and not future terms (for example, “add” instead of “added”). However, it’s usually only possible if commits are frequent. There’s only so much “how did I do it” you can remember when you’re faced with a deadline. Yet, well-written commits not only help collaborators, but are also helpful to the committer in recollecting history.

Chris Okpada

Don’t rely on links

One thing I like to remind colleagues of is that you’re not just explaining to the people who are going to decide whether to approve your commit. You’re also explaining to future developers and users who have found this commit in a bisect or blame operation and are trying to understand its relevance.

If the only context supplied is a link to some external system, and that far in the future the system it links to is no longer in use or has otherwise become inaccessible to that individual, your commit message has been rendered useless and may just as well be blank.

All too often, I go digging in the Git history of some open source project, and find commit messages which are nothing more than a bug ID or a link to some company’s internal and private defect tracker.

Don’t be that project!

Jeremy Stanley

Clear and concise changelogs

As a release communications manager, I often read the entire release board. I also met with developers to discuss any areas that weren’t clear yet. Then I tested the release early. After that, I would write a release post by sourcing the changelogs and corresponding revised or new content.

The changelogs are personal reminders for developers, but also have corresponding issues and tickets for them. You should capitalize product names appropriately, use a spell checker, be consistent with punctuation, and sentence structure. The lead developer should proofread these as well. Your customers, that are developers, are reading these. What information should they know before running the update to better serve their customers?

Courtney Robertson

Be specific

As a frequent release manager, I like messages that name the component a commit touches, and a brief description of what was changed. Also having a reference back to where the request for this work came from helps to tie fixes together long after we forgot about your clever branch name.

  • “fix fatal error” is not ideal.
  • “ISS-304: Fix fatal error in Login Access Control function for users
    with the Partner role” is better.
  • “ISS-304: Login Access Control: fix fatal error in getPartnerId()” is
    better still.

I can look at the entire relationship between a Git commit, branch, merge commit, and inspect the individual lines and files that were changed. But I don’t have that kind of time in the middle of a release. I want to be able to relate back to the source of this work in the project management tool, have some idea of which components are being changed, and in what way.

Ryan Price

Make it a habit

My favorite commit that I’m guilty of is, “commit before I switch branches” because I have to work on something else more urgent. Sometimes, I need to commit my current work to a totally different project. My manager’s strategy is to have us work as we normally do. But then when we rebase, he wants us to squash commits where it makes sense and write better messages. I can’t say we always do this, but his method does make sense.

I have a lot of “this is broken don’t know why” type messages too (haha) where I try things but want to commit that attempt before I try something else in case method A was closer to fixing the issue than method B. Writing code is a hot mess. And I’ve been writing it for over 10 years.

RachieVee

What commit message advice or tips do you live by? Let us know in the comments.

I asked our community of open source practitioners to share their advice for writing helpful Git commit messages.

woman on laptop sitting at the window

CC BY 3.0 US Mapbox Uncharted ERG


Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
49 points

Avatar
Milan, Italy

I’m a Software Engineer at Red Hat, working on CI/CD and Automation with cloud-native open-source technologies.

232 points

Portrait of Jeremy Stanley
Kill Devil Hills, NC, USA

A long-time computer hobbyist and technology generalist, Jeremy Stanley has worked as a Unix and GNU/Linux sysadmin for nearly three decades focusing on information security, Internet services, and data center automation. He’s a root administrator for the OpenDev Collaboratory, a maintainer of the Zuul project, and serves on the OpenStack vulnerability management team. Living on a small island in the Atlantic, in his spare time he writes free software, hacks on open hardware projects and embedded platforms, restores old video game systems, and enjoys articles on math theory and cosmology.

149 points

Avatar
India

I have more than 5 years of experience as a technical writer that specialises in producing accurate, clear, and concise documentation for software products. I have the ability to communicate technical ideas to a variety of audiences, including developers, engineers, and end users. Skilled in using a range of authoring tools, project management applications, and content management systems to produce and maintain technical documentation.

In addition to my technical proficiency, I also possess good interpersonal and communication abilities, which enable me to effectively engage with subject matter experts as well as cross-functional teams. Exceptionally organised and detail-oriented, with a passion for writing excellent documentation that aids users in comprehending and utilising sophisticated software products.

77 points

Miriam and her dog, Sasha, lying on the couch together.
Ottawa, Ontario, Canada

Miriam is a technical lead on the WordPress team at Kanopi Studios. She is a full-stack developer, leaning more toward the back end. She loves problem-solving, diving deep into plugin development, and mentoring junior developers.

Miriam is also heavily involved in the WordPress community, speaking and organizing WordCamps and local Ottawa meetups.

62 points

Leigh

Leigh is a long time open-source enthusiast, started nerding out with Linux kernel 2.0 and dial-up bulletin boards, can often be found hacking on useful code as well as being a product owner and entrepreneur.

Enjoys creating software that is used by people to solve real problems.

Always time for interesting and inspiring projects, feel free to each out – dgtlmoon@gmail.com

| Connect leighmorresi

31 points

Courtney Robertson profile photo
Chambersburg, PA

Courtney is a Web Designer and Developer Advocate, WordPress Training Team co-rep, and sponsored by GoDaddy Pro. Courtney has instructed WordPress and web development in career-technical high-schools, front-end bootcamps, and adult education. She began using open source in 1999 and contributing to WordPress in 2009.

| Connect courtneyr-dev

31 points

Picture of RachieVee

I’m a WordPress developer who has a keen interest in accessibility. #a11y I’m also a technical writer and I share my WordPress and coding experiences on my blog.

115 points

User profile image.

Lisa is a long time Linux user, former Gentoo developer (2003-2007). She is a lifelong gamer, which lead her to spend time in the video game industry and write one of the early API libraries for Eve Online (a video game involving the serious business of Internet space ships). Melding her experience as a long-time Linux systems administrator with software development experience, she is a Senior Site Reliability Engineer at Red Hat, focusing her time on OpenShift Dedicated (a hosted Kubernetes offering).

When not playing video games or tending the needs of her tuxedo cat Clyde, Lisa might be found playing Magic: the Gathering or tinkering with her ARM64 pet Kubernetes cluster, more of which can be read at https://lisa.dev.

Powered by WPeMatico

Author: dasuberworm

Standing just over 2 meters and hailing from о́стров Ратма́нова, Dasuberworm is a professional cryptologist, entrepreneur and cage fighter. When he's not breaking cyphers and punching people in the face, Das enjoys receiving ominous DHL packages at one of his many drop sites in SE Asia.

Share This Post On