A beginner's guide to Git (3 Part Series)
This was originally posted on: https://herewecode.io/blog/a-beginners-guide-to-git-how-to-write-a-good-commit-message/
You are a developer, and you started to use Git recently? If you are wondering what the best way to create a good commit message for your project is, then this article is made for you.
After a few weeks without writing new articles, I’m coming back today with the next part of one of my most searched posts on Google.
When I started to write, one year ago, I created an article on how to start and create your first repository with Git.
This article is appreciated by the community and helps many people each week. Thus, the next part of this series will be how to write a good commit message.
As a short reminder, the commit message is the short text you left when you save your work on Git. This message has the specific goal of identifying your work.
Just by reading it, anyone will be able to clearly understand what you did in this part of your work.
Below, you can find an example of the last commits on Angular.js project on GitHub (a famous framework made by Google). As you can see, messages are clear, and we can better understand which work has been done in different parts.
For example, on July 24, 2019, “gkalpak” upgraded “SauceConnect” and switched to the last version of Safari (web browser).
Git commits history on Angular.js project on GitHub
Unfortunately, commits do not have a universal way to be written. Each project and each company define specific rules for their needs.
Don’t worry; you will often find similar ways of writing the message.
Although, I highly recommend you to read the rules before starting in a company or working on an open-source project.
I will give you more details on these guidelines later in this article.
I created a shortlist of the advantages of using a good commit message.
- A better collaboration: If you are working in a company, on an open-source project, it’s essential to follow the rules for better cooperation. If you write something understandable, following the rules of the project, the other developers will be able to work more efficiently, and they will not be required to ask you questions about your work.
Note: If you are working on a personal project, I highly recommend you to follow specific rules. It will improve your productivity, and if you ask help from another developer, it will be easier for him to start working on your project.
- A better understanding: You need to create clear and understandable messages; it will help you and your collaborator to work on a project. Below, you can find an example of a git commit history with only unclear messages. As you can see, it’s difficult to understand what is made.
Bad git commits example from Jason McCreary
Note: If you want to have more example of bad commits and have fun at the same time, a Twitter account named "gitlost" is tweeting every day with the funny and unfiltered commits.
- BONUS - A changelog generation: I’ll tell you a small secret, if you write good messages, you will be able to generate changelog directly from your commit messages. I share with you a few tools to do it: Github Changelog Generator, Git Chglog, Auto Changelog, and Conventional Changelog. Probably the next part of the series will be on this, if you want to be updated, you can follow me on Twitter. I post every update there.
If you want to write a good commit message, you need to follow some rules. I created a checklist below. Every time you want to commit, take the time to refer to this list.
- Always check your grammar. It's never pleasant to read a message full of errors. To do this, I recommend you to use a grammar tool. If you are writing in English, you can use Grammarly, Reverso, or GrammarCheck. These tools are not perfect, but they will remove most of your errors.
- One commit, one change. Try to commit often. The ideal is to have each change in a different commit. It will be easier for you to go back to previous work.
- Be clear. When you are writing a commit, try to be as transparent as possible. I recommend you to use simple English to go straight to the point.
- Details what you did. Take the time to reread your code to write what you did. In the case you need to detail a lot, I recommend you to use the description part of the commit.
ADDITIONAL INFORMATION: I want to share with you more details on the 'git commit' command. If you are not using a git software, you should know that you can create detailed commit by typing this command:
$ git commit -m "Title" -m "Description"
It's the same as before, but with a second part for the description. So, "-m 'title' "is to write the short title of the commit, and "-m 'description' "is to write the description if you need to give more details.
- Use a git guideline. If you want to have a clear git commit history, I recommend you to follow a guideline. It's a guide of how to commit. In my case, I choose this simple one from Udacity. There are a lot of others; a few of them are Conventional Commits, and Angular Guideline. A commit guideline will help you to put an architecture to your commit. For example, put a tag to clarify what you did: "git commit -m fix: correctly delete all user information when the button delete account is triggered'".
I hope that you liked this guide on how to commit to Git! If you have any questions or feedback, feel free to ask.
If you have any other tips on how to make good commits, please let me know.
You can find other articles like this on my website: herewecode.io.
Level up every day