DEV Community

Cover image for Patterns for writing better git commit messages
Helder Burato Berto
Helder Burato Berto

Posted on • Edited on • Originally published at helderberto.com

Patterns for writing better git commit messages

It's an opinionated guide I keep with me to consult every time I catch myself in doubt if I'm writing nice commit messages with context of what I'm delivering.


A good commit should complete the following sentence

A properly formed Git commit subject line should always be able to complete the following sentence:
If applied, this commit <will your subject line here>

Commit Sample

See an example of commit below:

[type](optional scope): [subject]

[optional body]

[optional footer]
Enter fullscreen mode Exit fullscreen mode

Types

Must be one of the following:

  • build - Build related changes
  • ci - CI related changes
  • chore - Build process or auxiliary tool changes
  • docs - Documentation only changes
  • feat - A new feature
  • fix - A bug fix
  • perf - A code change that improves performance
  • refactor - A code change that neither fixes a bug or adds a feature
  • revert - Reverting things
  • style - Markup, white-space, formatting, missing semi-colons...
  • test - Adding missing tests

Scope

A scope may be provided to a commit’s type, to provide additional contextual information and is contained within parenthesis, e.g., feat(parser): add the ability to parse arrays.

Subject

The subject contains a succinct description of the change:

  • Use the imperative, present tense: "change" not "changed" nor "changes"
  • No dot (.) at the end.

Body

Just as in the subject, use the imperative, present tense: "change" not "changed" nor "changes". The body should include the motivation for the change and contrast this with previous behavior.

The 7 rules of a great commit message:

  1. Separate subject from the body with a blank line
  2. Limit the subject line to 50 characters
  3. Summary in the present tense. Not capitalized.
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how

Commit Template

Go to my git commit template

References

Oldest comments (41)

Collapse
 
karranb profile image
Karran Besen

Really useful article! I end up not focusing on the commit messages as much as I should. I'll try to follow these rules from now on.

Collapse
 
helderberto profile image
Helder Burato Berto

Thanks for the reply!
I'm glad the tips were helpful.

Collapse
 
duncandubblecloud profile image
duncan-dubblecloud

Great article.

Collapse
 
helderberto profile image
Helder Burato Berto

Thanks!

Collapse
 
bsignx profile image
Bruno Mariano

Very nice! Parceiro.

Collapse
 
helderberto profile image
Helder Burato Berto

Obrigado! :)

Collapse
 
lawson profile image
Lawson • Edited

Great

Collapse
 
helderberto profile image
Helder Burato Berto

Thanks!

Collapse
 
jannikwempe profile image
Jannik Wempe

Great, thanks. I personally would appreciate 2-3 examples for clarification 😊

Collapse
 
helderberto profile image
Helder Burato Berto

Nice to know that! I'll share more examples to clarify.
Thanks for your reply.

Collapse
 
billraymond profile image
Bill Raymond

These are great ideas. Could you consider a post with some solid examples to go with these practices?

Collapse
 
helderberto profile image
Helder Burato Berto

Thanks for the feedback.
Yeah! I'm thinking to make a more structured post with these topics, I created this one as a note in my blog and just shared here to see opinions about it.

Collapse
 
billraymond profile image
Bill Raymond

Thanks!

Collapse
 
qoomon profile image
Bengt Brodersen
Collapse
 
billraymond profile image
Bill Raymond

Very nice. Thank you!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Good article thanks for sharing these tips.

Collapse
 
helderberto profile image
Helder Burato Berto

Thanks! :)

Collapse
 
vishnuharidas profile image
Vishnu Haridas

Good ideas. Adding examples will be good to see.

Here are my rules that I stick on to:

  • Use simple present - Update login button to use the new API
  • Begin fixes with "Fix" and include ticket number if possible - Fix #234: User cannot log in with email
  • If pausing the work today and planning to continue tomorrow, start with [WIP] (Work-In-Progress) - [WIP] improving the login process
  • Begin temporary commits (usually for testing out something) with [TEMP] - [TEMP] add a temporary logout button
Collapse
 
helderberto profile image
Helder Burato Berto

Nice tips!
The unique one I try to maintain in the body is the ticket ID because in my case it's just to be tracked in Jira and isn't to be shown.

Collapse
 
qoomon profile image
Bengt Brodersen

I absolutely agree to your approach. I've created a highly configurable commit message checker to ensure the right format and valid types and scopes. See github.com/qoomon/git-conventional....

Collapse
 
helderberto profile image
Helder Burato Berto

That's really nice, I'll check it.
Thanks for the share.

Collapse
 
hectorip profile image
Héctor Iván Patricio Moreno

Great article! Would you mind if I translate this to Spanish in my blog?

Collapse
 
helderberto profile image
Helder Burato Berto

I really appreciate that, no problem, just mention the original post. :)