Introduction
In software development, we use Git to track and manage code changes. Each commit records the code modification, commit message, and details. Clear and specific commit messages help team members quickly understand the reasons for changes and the specifics of the modification, which is crucial for ensuring efficient code management and smooth team collaboration.
During development, small features often undergo multiple tweaks before being merged into the main branch. Because changes can be minor, developers may neglect writing clear commit messages or use overly brief titles—or worse, omit them entirely. As the number of commits increases, it becomes harder to track the specifics of each change, affecting future maintenance and debugging.
Therefore, it’s recommended to break down changes into finer details when modifying multiple files and ensure each commit reflects the specific change in a timely manner. At this point, using language tools like GPT can quickly analyze the changes and generate consistent, detailed commit messages, ensuring clear documentation for each commit.
There isn’t a single correct way to write commit messages. Different companies may have different preferences and standards, and developers may have their habits. Therefore, writing standardized commit messages ensures consistency and readability during communication among developers.
Conventional Commits
Conventional commit messages can unify the format as much as possible, enhancing readability and consistency. Without considering company-specific rules, the industry generally follows common conventions, which usually include the following points:
|
|
This format isn’t absolute, but a good commit message structure generally includes the type
, description
, and body
to describe the commit.
Type
The type is the most important part of a commit message, describing the type of change made. Common types include:
Type | Description |
---|---|
feat | New feature |
fix | Bug fix |
docs | Documentation update |
style | Changes that do not affect code logic (e.g., formatting) |
refactor | Code refactoring (neither a feature nor a bug fix) |
test | Adding or updating tests |
chore | Build processes or auxiliary tools changes |
perf | Performance improvement |
ci | Changes related to continuous integration |
build | Changes affecting build systems or dependencies |
There can be other types as well, such as deprecate
or release
, or even custom ones like post
for publishing a new article in my Hugo blog management.
Scope
Scope, added after the type, specifies the area or module affected by the change. This part is optional, providing more detail about the change. Examples:
feat(ui): Add new UI feature
fix(api): Fix bug in API
Description / Subject
The subject follows the type and scope and briefly summarizes the specific change in the commit. By convention, the subject is written in the imperative and kept under 50 characters, describing the code changes in a brief summary. This helps other developers quickly understand the purpose and content of the commit.
⚠️ No period at the end of the subject.
Examples:
feat(auth): Add OAuth2 authentication mechanism
fix(database): Fix database connection timeout issue
Body
The body starts one line after the subject and explains the details and context of the commit in a clear and detailed way. It can include:
- The reason for the change
- What issue was fixed or what feature was added
- Any relevant technical details or considerations
- Potential impacts (e.g., compatibility with other modules)
The body can be formatted using Markdown for better readability, such as using lists or code snippets. Each line should ideally stay within 72 characters for better readability in email clients or other tools.
How Do Others Write?
Let’s see how others write 😉.
Based on the examples below, some common rules can be summarized:
- Concise title, usually under 50 characters, using present-tense verbs without punctuation.
- The body elaborates on the changes if needed, referencing related issue numbers or review systems.
- Standardized format (some projects use type labels).
- Differences in type labels. Some use square brackets for the type, colons for separating the module from the description, and slashes to split the module and subsystem identifiers.
- Signatures to indicate responsibility, especially in open-source projects.
If you’ve modified and committed many files and only write fix login.py
, it will be very difficult to trace and maintain. You should explain the reasons properly, depending on what kind of developer you want to be.
- Ubuntu/OpenSSH 2b1ac6
|
|
- apple/ml-stable-diffusion 5d5f15
|
|
- apple/ml-stable-diffusion cf4e1e
|
|
- facebook/facebook-ios-sdk 21b018
|
|
- facebook/react 23cd3a
|
|
- AdGuardHome af0f43
|
|
- Tailscale 9a7346
|
|
⭐ Using GPT to Quickly Generate Commit Messages
Once we’re familiar with standardized commit messages, we can apply this in practice, using GPT to automate the process. Here’s how to automate this with Git and VS Code.
First, stage your code changes in VS Code.
Next, open the terminal below and enter the following command to generate the diff log of the staged files and pipe the result directly into the VS Code editor.
|
|
You can also add some prompt text to quickly copy and paste into GPT for generating commit messages.
For Windows with prompt:
|
|
For Linux with prompt:
|
|
VS Code will open a new window with the prompt and diff content. You can copy and paste this into GPT to generate a standardized commit message.
A GPT input example—here I write a more detailed prompt for consistent results:
|
|
GPT might generate the following commit message based on the above content:
|
|
By using this method, we can quickly generate submissions that meet the standards, ensuring the quality of the submission.
Avoid Using Gitmoji
In the past, I used Gitmoji, and these visually appealing emojis did add some fun to otherwise tedious commit messages.
However, I encountered errors when converting commits to patches and applying those patches internally. I don’t recall the exact reason, but it was likely due to the symbols at the beginning of the commit messages (such as colons), which caused older versions of Git to fail during the apply process. Therefore, Gitmoji poses compatibility issues, and its use is not highly recommended.
Additionally, Gitmoji lacks consistent guidelines. Your interpretation of an emoji may differ from mine, leading to potential misunderstandings. For example:
- 🐛 is typically used for bug fixes, while 🚑️ is used for urgent bug fixes, but the distinction between them is unclear.
- 🔥 is for code removal. If a bug 🐛 causes compilation failure and removing the code solves the problem, should you use 🔥 or 🐛?
- 🔒️ represents security fixes. If the security issue is essentially caused by a bug 🐛, which one should you choose?
These are just a few of the concerns that arise when using Gitmoji, and there are many other potential confusions that I won’t list here.
It is clear that writing concise and well-structured commit messages is far more effective in conveying precise information and intent than using an emoji that might not resonate with others.
Moreover, by adhering to standardized commit message structures with clear type
and scope
, it becomes easier to convey the nature of the changes and the parts of the project being affected.
Conclusion
Writing clear and standardized Git commit messages is essential for code traceability, version control, and teamwork. Good commit messages help developers quickly understand the changes and reduce maintenance difficulties. Using tools like GPT can help generate high-quality commit messages quickly, improving development efficiency and maintaining consistency in version control.
This article also provides examples of commit messages from various open-source projects, showcasing stylistic differences between developers. However, the overarching rules remain the same: specific types, clear and concise titles, detailed descriptions when needed, and references to relevant issues or identifiers for quick tracking.
At the same time, it’s best to avoid over-reliance on emoji-based commit messages (such as Gitmoji) to ensure compatibility and readability across different environments. Following good commit practices and standards promotes team collaboration and ensures the long-term stability of the codebase.
Effectively utilizing these tools and standards will help streamline the development process, ensuring that each change is correctly understood and managed, ultimately improving overall development quality.