How to Write Commit Messages?

MyParisa

Üye
31 Mar 2023
79
38

How to Write Commit Messages?
1*MMvv6yXKju2WUe26J3yJww.png

What does git commit do and why do we need it?

The “git commit” command takes a snapshot of the changes made, additions, deletions or modifications, in a git repository and saves them. This works similarly to the save function in computer games. When our project reaches a certain stage, we use the “git commit” command to record our progress. This allows us to revert to previous versions of the project and helps other developers we collaborate with to keep track of their work.

The easiest way to write a git commit message is to use the command git commit -m ‘your commit message’. This command takes a snapshot of the changes made and saves them to the repository with an explanatory message called the “commit message”, indicating what these changes are. This allows you to track information such as when and why changes were made to the repository and revert to previous versions of the project. Additionally, clear and descriptive commit messages can help other developers working on the project to track their work and facilitate future development of the project.

What is the message of Commit?

A commit message is an explanatory text added during a Git commit process and is included by the developer who writes the commit to the commit object. Commit messages are used to explain what changes were made in the project and why they were made. This allows future readers or other developers to understand the changes and keep track of the project’s history.

Commit messages are also important for understanding the code we write. When we look back at our code at a later time, commit messages help us to better understand the old code we wrote. Commit messages usually have a title and an optional body. The title includes a summary of the changes while the body can provide more detailed information. Accurate and descriptive commit messages are crucial for tracking the development of the project and sharing it with other developers.

Why should we create a quality commit message?

Our commits are snapshots of our code changes, stored along with commit messages that explain the changes made. Therefore, well-written commit messages help others understand what our code does when they look back at it later. If we write bad or insufficient commit messages, we may need to look back at old commit messages to find errors in our code. In this case, we may need to read through all of the code to find errors, which can be time-consuming and laborious. Additionally, understanding what code written months ago does can be difficult. Well-written commit messages make it easy to find where errors are and understand what our code does. This way we can fix bugs and make our code better.

General rules for writing commit messages

As a general rule, commit messages should be short, descriptive, and a single line that does not exceed 50 characters in the header section. Then, a blank line should be left and a more detailed explanatory text can be written optionally. If there are multiple changes in your commit, it may be difficult to write a single short message. In this case, you can make your job easier by splitting the changes into multiple commits. For example, if you have made 3 changes in a commit, 2 of them are working fine but one is causing an error in the future, when you revert to the original state before committing to fix the error, you may have to remove the working 2 changes as well. To avoid this situation, it is recommended to commit changes in small pieces as much as possible.

1. Keep it short and concise
  • Having fewer changes in a commit will help you write a shorter message.
  • To make your message clear and understandable, focus on a single topic and keep it as simple as possible.
2. Use the present tense and imperative mood
  • Use the present tense and imperative mood in your commit messages, such as “Make x method asynchronous”.
3. Add a short title
  • The title should be less than 50 characters.
  • Most code management systems provide a limited space for commit message titles, which is generally limited to 50 characters. Therefore, your message should be less than 50 characters to be fully visible.
  • Having a short title helps other team members quickly understand the changes.
4. Add an optional body
  • There should be a blank line between the header and the body.
  • The body should be no more than 72 characters per line.
  • Write a message explaining what and why the change is needed.
  • This section is where we describe what the change is, why it was made, and the impact it has on the project.
  • Be as detailed as possible in describing what the change is.
  • Avoid unnecessary details.
5. Avoid large size commit messages
  • A message that is 100mb in size will definitely cause performance issues.

Additionally, a footer can be added, which usually includes an external reference identifier such as a Jira or issue number.

Bad example of committing:

Kod:
Fix some bugs

An example of good commit:

Kod:
feat: Add user authentication feature

This commit message does not clearly describe what the change is and does not specify which issues it fixes. Such a commit message can make it difficult to trace this change back in the future.

Semantic commit

Semantic commit messages provide a clear and concise structure that describes the purpose and actions performed in a commit. They improve the readability of commit messages and make it easier to track changes.

A semantic commit message consists of a prefix added to the commit message header and a short description. You can also specify the section where the change was made in parentheses.

Let’s look at the following commit as an example.

Kod:
feat(login): Add support for user login

In the above commit, the “feat” suffix indicates that a new feature has been added. “login” indicates in which section the addition is made.

The general structure of the Semantic commit is as follows.

Kod:
feat: add hat wobble
^--^  ^------------^
|     |
|     +-> Summary in present tense.
|
+-------> Type: chore, docs, feat, fix, refactor, style, or test.

These are the common semantic commit types:

  • feat: an abbreviation for “feature” and used when a new feature is added.
  • fix: used for bug fixes.
  • docs: used for documentation changes.
  • style: used for changes in the code style.refactor: used for improving, restructuring, or editing existing code.
  • test: used for changes related to tests.
  • chore: used for changes related to code maintenance or configuration.
For more details see: https://gist.github.com/joshbuchea/6f47e86d2510bce28f8e7f42ae84c716

Below are a few examples of commitments:

Kod:
feat: Add pagination to user list page

This commit adds pagination functionality to the user list page,
allowing users to view a large number of users in smaller, more
manageable groups. The pagination controls are located at the bottom
of the page and allow the user to navigate through the different pages.

The following changes were made:
- Added pagination functionality to user list page
- Updated styling for pagination controls

No issues closed

Kod:
fix: Resolve issue with user profile image upload

This commit fixes an issue where user profile images could not be
uploaded due to a server error. The issue was caused by an
incorrect file path in the upload function, which has now been
corrected.

The following changes were made:

Updated file path in upload function to correct path
Tested and verified image upload functionality
Issue #1234 closed

Kod:
feat(requests): Add retry functionality for failed requests

This commit adds retry functionality to the requests module, allowing
failed requests to be retried automatically. The retry mechanism uses
an exponential backoff algorithm to gradually increase the time between
retries, preventing overwhelming the server with repeated requests.

The following changes were made:
- Added retry functionality to requests module
- Implemented exponential backoff algorithm for retry interval

No issues closed

Teams with git ticket system

Most teams use “Jira” and similar ticket systems that assign a ticket number to a specific work section.

When writing commit messages, if you use a ticketing or tracking system for project management, it can be useful to use ticket numbers in your commit messages.

For example, for a Jira ticket “ACME-1”, it is common practice to include the ticket number at the beginning of the commit as follows.

Kod:
[ACME-1] Enable Logging Globally

Here, the ticket number “ACME-1” clearly indicates for which ticket the change was made.

Using ticket numbers in commit messages not only helps other developers understand which tickets the changes are related to, but also makes it easier for project management to keep track of tickets. Thus, using ticket numbers increases the traceability and manageability of the project.

Sources I used:

Kod:
1. https://medium.com/@steveamaza/how-to-write-a-proper-git-commit-message-e028865e5791
2. https://initialcommit.com/blog/git-commit-messages-best-practices
3. https://www.gitkraken.com/learn/git/best-practices/git-commit-message
4. https://www.microverse.org/blog/how-writing-meaningful-commit-messages-helps-you-be-a-better-developer
5. https://chiamakaikeanyi.dev/how-to-write-good-git-commit-messages/
6. https://github.blog/2022-06-30-write-better-commits-build-better-projects/
7. https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/
8. https://cbea.ms/git-commit/
9. http://who-t.blogspot.com/2009/12/on-commit-messages.html
10. https://corgibytes.com/blog/2019/03/20/commit-messages/
11. https://yvonnickfrin.dev/a-guide-on-commit-messages
12. https://hackernoon.com/power-up-your-pair-programming-with-co-authored-commits-on-github-ffb5d049aed3
13. https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors
14. https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
15. https://medium.com/ltunes/git-commit-mesajlar%C4%B1m%C4%B1z-nas%C4%B1l-olmal%C4%B1-fdf3ae323672
16. https://oguzhanturan.medium.com/i%CC%87yi-bir-git-commit-mesaj%C4%B1-nas%C4%B1l-yaz%C4%B1l%C4%B1r-3b0cbc07377b
17. https://vigo.github.io/git-puf-noktalari/bolum-01/13-commit-mesaji-nedir/
18. https://www.conventionalcommits.org/tr/v1.0.0/
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.