Git Product home page Git Product logo

commit-messages-guide's Introduction

Commit messages guide

Say Thanks!

A guide to understanding the importance of commit messages and how to write them well.

It may help you to learn what a commit is, why it is important to write good messages, best practices and some tips to plan and (re)write a good commit history.

Available languages

What is a "commit"?

In simple terms, a commit is a snapshot of your local files, written in your local repository. Contrary to what some people think, git doesn't store only the difference between the files, it stores a full version of all files. For files that didn't change from one commit to another, git stores just a link to the previous identical file that is already stored.

The image below shows how git stores data over time, in which each "Version" is a commit:

Why are commit messages important?

  • To speed up and streamline code reviews
  • To help in the understanding of a change
  • To explain "the whys" that cannot be described only with code
  • To help future maintainers figure out why and how changes were made, making troubleshooting and debugging easier

To maximize those outcomes, we can use some good practices and standards described in the next section.

Good practices

These are some practices collected from my experiences, internet articles, and other guides. If you have others (or disagree with some) feel free to open a Pull Request and contribute.

Use imperative form

# Good
Use InventoryBackendPool to retrieve inventory backend
# Bad
Used InventoryBackendPool to retrieve inventory backend

But why use the imperative form?

A commit message describes what the referenced change actually does, its effects, not what was done.

This excellent article from Chris Beams gives us a simple sentence that can be used to help us write better commit messages in imperative form:

If applied, this commit will <commit message>

Examples:

# Good
If applied, this commit will use InventoryBackendPool to retrieve inventory backend
# Bad
If applied, this commit will used InventoryBackendPool to retrieve inventory backend

Capitalize the first letter

# Good
Add `use` method to Credit model
# Bad
add `use` method to Credit model

The reason that the first letter should be capitalized is to follow the grammar rule of using capital letters at the beginning of sentences.

The use of this practice may vary from person to person, team to team, or even from language to language. Capitalized or not, an important point is to stick to a single standard and follow it.

Try to communicate what the change does without having to look at the source code

# Good
Add `use` method to Credit model

# Bad
Add `use` method
# Good
Increase left padding between textbox and layout frame
# Bad
Adjust css

It is useful in many scenarios (e.g. multiple commits, several changes and refactors) to help reviewers understand what the committer was thinking.

Use the message body to explain "why", "for what", "how" and additional details

# Good
Fix method name of InventoryBackend child classes

Classes derived from InventoryBackend were not
respecting the base class interface.

It worked because the cart was calling the backend implementation
incorrectly.
# Good
Serialize and deserialize credits to json in Cart

Convert the Credit instances to dict for two main reasons:

  - Pickle relies on file path for classes and we do not want to break up
    everything if a refactor is needed
  - Dict and built-in types are pickleable by default
# Good
Add `use` method to Credit

Change from namedtuple to class because we need to
setup a new attribute (in_use_amount) with a new value

The subject and the body of the messages are separated by a blank line. Additional blank lines are considered as a part of the message body.

Characters like -, * and ` are elements that improve readability.

Avoid generic messages or messages without any context

# Bad
Fix this

Fix stuff

It should work now

Change stuff

Adjust css

Limit the number of characters

It's recommended to use a maximum of 50 characters for the subject and 72 for the body.

Keep language consistency

For project owners: Choose a language and write all commit messages using that language. Ideally, it should match the code comments, default translation locale (for localized projects), etc.

For contributors: Write your commit messages using the same language as the existing commit history.

# Good
ababab Add `use` method to Credit model
efefef Use InventoryBackendPool to retrieve inventory backend
bebebe Fix method name of InventoryBackend child classes
# Good (Portuguese example)
ababab Adiciona o método `use` ao model Credit
efefef Usa o InventoryBackendPool para recuperar o backend de estoque
bebebe Corrige nome de método na classe InventoryBackend
# Bad (mixes English and Portuguese)
ababab Usa o InventoryBackendPool para recuperar o backend de estoque
efefef Add `use` method to Credit model
cdcdcd Agora vai

Template

This is a template, written originally by Tim Pope, which appears in the Pro Git Book.

Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together.

Explain the problem that this commit is solving. Focus on why you
are making this change as opposed to how (the code explains that).
Are there side effects or other unintuitive consequences of this
change? Here's the place to explain them.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded
   by a single space, with blank lines in between, but conventions
   vary here

If you use an issue tracker, put references to them at the bottom,
like this:

Resolves: #123
See also: #456, #789

Rebase vs. Merge

This section is a TL;DR of Atlassian's excellent tutorial, "Merging vs. Rebasing".

Rebase

TL;DR: Applies your branch commits, one by one, upon the base branch, generating a new tree.

Merge

TL;DR: Creates a new commit, called (appropriately) a merge commit, with the differences between the two branches.

Why do some people prefer to rebase over merge?

I particularly prefer to rebase over merge. The reasons include:

  • It generates a "clean" history, without unnecessary merge commits
  • What you see is what you get, i.e., in a code review all changes come from a specific and entitled commit, avoiding changes hidden in merge commits
  • More merges are resolved by the committer, and every merge change is in a commit with a proper message
    • It's unusual to dig in and review merge commits, so avoiding them ensures all changes have a commit where they belong

When to squash

"Squashing" is the process of taking a series of commits and condensing them into a single commit.

It's useful in several situations, e.g.:

  • Reducing commits with little or no context (typo corrections, formatting, forgotten stuff)
  • Joining separate changes that make more sense when applied together
  • Rewriting work in progress commits

When to avoid rebase or squash?

Avoid rebase and squash in public commits or in shared branches where multiple people work on. Rebase and squash rewrite history and overwrite existing commits, doing it on commits that are on shared branches (i.e., commits pushed to a remote repository or that comes from others branches) can cause confusion and people may lose their changes (both locally and remotely) because of divergent trees and conflicts.

Useful git commands

rebase -i

Use it to squash commits, edit messages, rewrite/delete/reorder commits, etc.

pick 002a7cc Improve description and update document title
pick 897f66d Add contributing section
pick e9549cf Add a section of Available languages
pick ec003aa Add "What is a commit" section"
pick bbe5361 Add source referencing as a point of help wanted
pick b71115e Add a section explaining the importance of commit messages
pick 669bf2b Add "Good practices" section
pick d8340d7 Add capitalization of first letter practice
pick 925f42b Add a practice to encourage good descriptions
pick be05171 Add a section showing good uses of message body
pick d115bb8 Add generic messages and column limit sections
pick 1693840 Add a section about language consistency
pick 80c5f47 Add commit message template
pick 8827962 Fix triple "m" typo
pick 9b81c72 Add "Rebase vs Merge" section

# Rebase 9e6dc75..9b81c72 onto 9e6dc75 (15 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into the previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

fixup

Use it to clean up commits easily and without needing a more complex rebase. This article has very good examples of how and when to do it.

cherry-pick

It is very useful to apply that commit you made on the wrong branch, without the need to code it again.

Example:

$ git cherry-pick 790ab21
[master 094d820] Fix English grammar in Contributing
 Date: Sun Feb 25 23:14:23 2018 -0300
 1 file changed, 1 insertion(+), 1 deletion(-)

add/checkout/reset [--patch | -p]

Let's say we have the following diff:

diff --git a/README.md b/README.md
index 7b45277..6b1993c 100644
--- a/README.md
+++ b/README.md
@@ -186,10 +186,13 @@ bebebe Corrige nome de método na classe InventoryBackend
 ``
 # Bad (mixes English and Portuguese)
 ababab Usa o InventoryBackendPool para recuperar o backend de estoque
-efefef Add `use` method to Credit model
 cdcdcd Agora vai
 ``

+### Template
+
+This is a template, [written originally by Tim Pope](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), which appears in the [_Pro Git Book_](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project).
+
 ## Contributing

 Any kind of help would be appreciated. Example of topics that you can help me with:
@@ -202,3 +205,4 @@ Any kind of help would be appreciated. Example of topics that you can help me wi

 - [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/)
 - [Pro Git Book - Commit guidelines](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines)
+- [A Note About Git Commit Messages](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)

We can use git add -p to add only the patches we want to, without the need to change the code that is already written. It's useful to split a big change into smaller commits or to reset/checkout specific changes.

Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? s
Split into 2 hunks.

hunk 1

@@ -186,7 +186,6 @@
 ``
 # Bad (mixes English and Portuguese)
 ababab Usa o InventoryBackendPool para recuperar o backend de estoque
-efefef Add `use` method to Credit model
 cdcdcd Agora vai
 ``

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?

hunk 2

@@ -190,6 +189,10 @@
 ``
 cdcdcd Agora vai
 ``

+### Template
+
+This is a template, [written originally by Tim Pope](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), which appears in the [_Pro Git Book_](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project).
+
 ## Contributing

 Any kind of help would be appreciated. Example of topics that you can help me with:
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]?

hunk 3

@@ -202,3 +205,4 @@ Any kind of help would be appreciated. Example of topics that you can help me wi

 - [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/)
 - [Pro Git Book - Commit guidelines](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines)
+- [A Note About Git Commit Messages](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)

Other interesting stuff

Like it?

Say thanks!

Contributing

Any kind of help would be appreciated. Example of topics that you can help me with:

  • Grammar and spelling corrections
  • Translation to other languages
  • Improvement of source referencing
  • Incorrect or incomplete information

Inspirations, sources and further reading

commit-messages-guide's People

Contributors

afn-korni avatar akadir avatar ana06 avatar cefigueiredo avatar coder591 avatar dgw avatar dmitriy-vas avatar edoardottt avatar esmaeilpour avatar everdimension avatar faxemaxee avatar glancu avatar hong4rc avatar johnjohndoe avatar krzysztofpa avatar lex111 avatar lploc94 avatar lucasmoreiradev avatar nogut0123 avatar nqvuong1998 avatar preco21 avatar r2p2 avatar relliv avatar romulooliveira avatar ryoon avatar seriouszyx avatar tiagobarreto avatar tompatib avatar wang-steven avatar xlxs4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

commit-messages-guide's Issues

Add Table-of-Content section

As mentioned in #50 the whole project would have much more readability, if a TOC is added.

Since it is easier for new readers to follow along the topics of the chapters and it provides a better navigation for readers, that want to lookup a single point, it would improve the quality of the whole project.

Improvements

I really like the guide, it is really complete and I would like to link it from some of my projects. I think there is still place for some improvements.

Some things taken from https://github.com/erlang/otp/wiki/writing-good-commit-messages:

  • Always leave the second line blank.
  • Don't end the summary line with a period - it's a title and titles don't end with a period.

Some others:

This are also standard thing, so would be great to add them. Are you up for a PR? I have the felling that every of us is writing is own commit guide in his projects, while we could have one general one. At the end we all write the same things. It would be nice to have some kind of community guide. It would be better and it will save all of us time. 😉

Update Portuguese transalation

The portuguese version was written before the main English version that is available in this repository.

Things that need update:

  • More detailed explanations in some sections
  • Better source referencing
  • Double checks of missing translation or typos

Use of Git Log and Status

Hi,

Great guide! The source material gives a lot more context to this piece. You've covered every aspect on commits, but I think a little bit more emphasize on usage of git log and git status would make a huge difference. Though this is mentioned in the source materials, having this before rebase and merge will provide more clarity.
Adding shortcuts for logs would enable insights to effectively rebase or merge commits as well.
A few shortcuts you could add,
git log --oneline
git log --all
git log --graph
git log --stat
git log -p

url instead of issue number

I find in general a good practise to write in the commit message

Closes https://github.com/RomuloOliveira/commit-messages-guide/issues/32

Instead of

Closes #32

What do do you think? should we update this in the guide?

Add gitmoji to "Other interesting stuff"

Thanks for putting this guide together! We've been using emoji in our commit messages for an easy shorthand way to see what a commit is about. It's fun, and helps me break down what I've done in the commit. In addition, it lets me know when I'm maybe making too large of a commit, since it's best limited to two emoji. The author of the repo writes:

Using emojis on commit messages provides an easy way of identifying the purpose or intention of a commit with only looking at the emojis used. As there are a lot of different emojis I found the need of creating a guide that can help to use emojis easier.

https://gitmoji.carloscuesta.me/

I thought it might fit under the "Other interesting stuff" section.

Thanks for considering!

Improve "Checkins over time" image

I think it would make more sense that under the section 'What is a "commit"?' the image (png) "Checkins over time" should have the numbers in the "Version" (top row) match up with the commit files. For example under, "Version 2" we have commits/files labeled "A1" and "C1".
I think it would be more intuitive and clearer for the reader to have the numbers be the same.

Add some details about setting up a git commit template

I think it will be really helpful if some details about adding a git commit template was added. This way when they do git commit the editor will pop up with the template inside.

For instance:

  • ~/.gitcommit.template

# Summarize changes in around 50 characters or less
#
# Write the messages in imperative form:
# If applied, this commit will <commit message>
#
# Example:
#  If applied, this commit will use InventoryBackendPool to retrieve inventory backend
# 
# More detailed explanatory text, if necessary. Wrap it to about 72
# characters or so. In some contexts, the first line is treated as the
# subject of the commit and the rest of the text as the body. The
# blank line separating the summary from the body is critical (unless
# you omit the body entirely); various tools like `log`, `shortlog`
# and `rebase` can get confused if you run the two together.
# 
# Explain the problem that this commit is solving. Focus on why you
# are making this change as opposed to how (the code explains that).
# Are there side effects or other unintuitive consequences of this
# change? Here's the place to explain them.
# 
# Further paragraphs come after blank lines.
# 
#  - Bullet points are okay, too
# 
#  - Typically a hyphen or asterisk is used for the bullet, preceded
#    by a single space, with blank lines in between, but conventions
#    vary here
# 
# If you use an issue tracker, put references to them at the bottom,
# like this:
# 
# Resolves: #123
# See also: #456, #789
  • Setting it up
git config --global commit.template ~/.gitcommit.template

Now, running git commit will always bring up that template loaded in the editor.

Does not mention the drawbacks of rebase and squashes

I really like the guide. My only concerns are on the recommendation of squashes and rebases without mention the drawbacks of change the history.

May I patch some comments on this subject?

If I do, I will need help translating it to Deutsch.

[PROPOSAL] Renaming project?

Thank you all for this nice guide! It's a good introduction for beginners to git.

Since it seems to become more a general git-guide than a "how to write good commit messages"-guide it may be better to rename the project.

It also may be better to reorganize some things, because the jump from "good commit message" to "rebase vs merge" was a bit confusing for me as a first-time reader. Maybe adding some kind of TOC or something...

What do you think about this proposal?

First line should summary why this commit was necessary

Hello.

Doing some Git log archeology quite often, I always struggle to find why a commit was done rather than what it does:

  • a first line to summary why this commit was done
  • a longer description to make explicit the context of the why given in the first line. This description summary how things were done.
  • if necessary, a list of files with a summary of what was wrong specifically to this file and the tricky parts

This way:

  • git log --oneline master shows why pull requests where accepted
  • git show aaafffeee shows what is done by a commit

So, instead of:

Use InventoryBackendPool to retrieve inventory backend

I think it's better to have

`Inventory` is slow and heavy for the backend

We call the backend several times and `Inventory` open and close a connection
at each call which is slow to execute and add load to the backend to open
connections and authenticate them.

We switch the part `Foo` of the software to `InventoryBackendPool` 
which open once a connection pool with the backend and maintain them open
and reopen them if they close unexpectidly.

Ref: #42

Regards.

Languages mixed (ITA)

I think this is wrong...If I understood well the guide haha.
Mantieni la lingua in modo consistente

Per i proprietari dei progetti: scegli una lingua e scrivi tutti i messaggi di commit in quella lingua. Sarebbe l'ideale se fosse la stessa lingua con cui si scrivono i commenti nel codice, la stessa predefinita per l'applicazione, ecc.

Per chi collabora: scrivi i messaggi dei tuoi commit rispettando la stessa lingua della history dei commit.

# Corretto
ababab Aggiunge il metodo use al modello Credit
efefef Usa InventoryBackendPool per recuperare il backend dell'inventario
bebebe Fix method name of InventoryBackend child classes

Here the languages are mixed, even the advice says that you have to use one single language.

Add vietnamese version for this guide

Hi, I see this guide is very helpful, but the truth that there are a few Vietnamese peoples know this guide. I want to translate this guide to Vietnamese so that it can be popular in Vietnamese developer community. I have read CONTRIBUTING.md carefuly. I hope I can help by adding a README_vi-VI.md file for Vietnamese version.

not follow the rules

Hi,
Thanks for your great guide, but I am just curious that why you do not follow some of the rules in you commit messages?

These should use the imperative form, isn't it?

#8b1a1f13a6

#8195c4c08

Remove Chris Beams 'help sentence' in favor of the imperative

Almost all sources regarding the commit summary message suggest using the imperative. Unfortunately, in his otherwise very nice and often cited article on commit message, will also advocating the imperative Chris Beams made the mistake to suggest that a commit message should also be able to complete his "help sentence": If applied, this commit will ....

As the imperative in English is always the same as the infinitive, imperative summaries used in this way will correctly complete the sentence with the english will future form (but will no longer be imperative).

This does not work at all in many (probably most) other languages, however. Although being cited on numerous places, it leads to confusion as soon as languages other then english are used (for example, in this guide, in the german version, the example sentences are like Korrigiert den Methodennamen der InventoryBackend Kind-Klassen which is not imperative (imperative would be 'Korrigiere den Methodennamen der InventoryBackend Kind-Klassen`).

As in other languages, using a help sentence as advocated by Chris beams would lead to grammatically totally different forms of summaries, I suggest completely removing the 'help sentence' from the guide.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.