What I've Learned About Git from Senior Colleagues (Part 2 - thoughful commit)
Tags: git, commit, github
Published: 2024-03-15 09:34:21

Welcome back to part 2 of the series 'What I've Learned About Git from Senior Colleagues.' In this installment, I want to discuss the importance of writing good commit messages. 'But can't I just write anything?' Well, technically, you can commit with any message :) . But have you ever seen a commit history that looks like this?
```bash
72ae8b3 Fixed bug
7f48a12 Refactor code
35b9f78 Update file
a9c0e17 Implement new feature
de97c5f Fixed a minor typo in the README.md file, added descriptive comments to improve code readability in main.py, and optimized database query performance by implementing caching
b3fd247 Completed task #123
8a71e6f Fixed bug reported by QA team
f2a6b41 Merge branch 'feature-branch' into 'main'
```
OK, 'fix bug' fixes which bug exactly? 'Refactor code' refactors which specific code section? 'Update file' updates which file? 'Implement new feature' is about what feature exactly? And do I really need to know every detail when you write 2983982374234 characters in a commit message?
If it's a feature you developed, you might still remember it when you look back a few weeks later. But a few months later? Who knows. Not to mention, others might need to find your commit too. That's why writing meaningful commits is essential. I was fortunate that the first project I joined enforced strict rules for all commits. However, the purpose of this article is not to force you to follow my rules. Because each project may have different commit guidelines. But even in projects with no specific rules, you still need to write meaningful commit messages.
At the end of this article, I'll share some rules from the projects I've been involved in. But for now, I'll highlight the core principles of writing meaningful commit messages.
## 1. The rules of a great Git commit message
### 1.1. Don't make the subject too long
Each project will have a specific limit for the commit subject, which could be 50, 70 characters, ... But even if the project doesn't specify, I think you should limit your commit subject to under 70 characters.
Look at this. Do you want your project to look like this?

![GitHub UI truncate commit message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8f0ncvkhxclhggfhobyv.png)
The GitHub UI will truncate your message at 72 characters. Therefore, you need to summarize your code changes as briefly as possible. If your commit handles many changes and you cannot summarize them, you can try splitting that one commit into many smaller commits.

### 1.2. Capitalize the first letter of the subject.
Try:
- Update: logic authorize at post page

Instead of:
- <del>update: logic authorize at post page</del>

### 1.3. Don't end your commit subject with a period. 
The subject is just a title, and titles don't need periods.
Try:
- Update: logic authorize at post page

Instead of:
- <del>Update: logic authorize at post page.</del>

These are the core principles to write good commit messages. Some places recommend writing a commit body as well, but for me, that's optional. You can include it in the description of the pull request.
## 2. Some rules from the projects I've been involved in. 
I was fortunate that the first time I joined a real project, it enforced strict rules for commit messages. These could be suggestions for you on how to set rules for your own project.
### 2.1. [Action]: [FE/BE] / [Subject]
Action could be:
- Add:
- Update:
- Delete:
- Fix:

Some examples:
- `Add: BE / Add API to get post`
- `Update: FE / Modify button's style on login form`

### 2.2. [emoji] [Subject]
You can see emoji list [here](https://gist.github.com/rxaviers/7360908)
Some examples:
- `:+1: Update API to get notifications` -> GitHub UI will show `👍 Update API to get notifications`
- `:up: Upgrade version DB` -> GitHub UI will show `🆙 Upgrade version DB`

## 3. Using AI to generate commit messages. 
VS Code has integrated the feature of using AI to generate commit messages based on your code changes. For example, if you have GitHub Copilot, you can go to the Version Control tab, click on the Copilot icon to see the result.

![Generate commit message with GitHub Copilot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4d86a55wfn5tdsg3z9y4.png)
Additionally, you can use other AI services such as ChatGPT 3.5, ChatGPT 4.0, etc., via the GitLens extension. I will have another article that provides more detailed instructions. Stay tuned for more updates and follow along for the latest insights.

In summary, writing meaningful commit messages is essential for effective version control. By following key principles like keeping messages concise and capitalizing the first letter, developers can enhance collaboration and project comprehension. Mastering this skill leads to clearer histories, smoother workflows, and improved professionalism. Happy coding!

Reference:
- [What I've Learned About Git from Senior Colleagues (Part 1 - git stash)](https://dev.to/doantrongnam/what-ive-learned-about-git-from-senior-colleagues-part-1-git-stash-18e3)
- [How to Write a Git Commit Message](https://cbea.ms/git-commit/)