Hướng dẫn Tạo Commit Chuyên nghiệp
1. Nguyên tắc Vàng: "Atomic Commits" (Commit Nguyên tử) ⚛️
Mỗi commit nên đại diện cho một thay đổi logic duy nhất, hoàn chỉnh và có thể hoạt động được. Tránh gộp nhiều thay đổi không liên quan vào một commit.
Ví dụ:
Thay vì commit lớn:
feat: Add Brand, Category and Product features
Hãy chia nhỏ thành các commit "nguyên tử":
feat(brand): Add Brand entity and repositoryfeat(category): Add Category entity with self-referencing relationshipfeat(product): Add Product entity with relations to Brand and Categoryfeat(product): Implement ProductService logictest(product): Add unit tests for ProductService
Lợi ích:
- Dễ dàng xem lại (Review): Dễ kiểm tra thay đổi nhỏ và cụ thể.
- Dễ dàng hoàn tác (Revert): Chỉ cần hoàn tác commit gây lỗi mà không ảnh hưởng chức năng khác.
- Lịch sử rõ ràng: Giúp hiểu rõ quá trình phát triển.
2. Cấu trúc một Commit Message Tốt: "Conventional Commits" 📝
Quy chuẩn này giúp tự động tạo changelog và giữ lịch sử Git nhất quán.
Cấu trúc:
<type>(<scope>): <subject>
a. type (Loại thay đổi):
feat: Tính năng mới (feature).fix: Sửa lỗi (bug fix).docs: Thay đổi tài liệu (documentation).style: Định dạng code (formatting, white-space...), không ảnh hưởng logic.refactor: Tái cấu trúc code, không thêm tính năng hay sửa lỗi.test: Thêm/sửa bài kiểm thử (test case).chore: Công việc vặt (ví dụ: cập nhật .gitignore, cấu hình build).build: Thay đổi hệ thống build (ví dụ: pom.xml).
b. scope (Phạm vi - Tùy chọn):
Chỉ định module/feature bị ảnh hưởng, ví dụ: (product), (user), (order), (security), (config).
c. subject (Tiêu đề):
- Viết ở thì hiện tại, dạng mệnh lệnh (ví dụ: "Add" thay vì "Added" hay "Adding").
- Viết hoa chữ cái đầu.
- Ngắn gọn (dưới 50 ký tự).
- Không có dấu chấm cuối.
3. Luồng làm việc Đề xuất (Suggested Workflow) 🚀
-
Tạo Branch mới: Luôn bắt đầu tính năng/sửa lỗi trên branch riêng. Đặt tên rõ ràng:
feature/add-brand-crudfix/login-authentication-bug
-
Làm việc và Commit thường xuyên: Sau mỗi thay đổi "nguyên tử", thực hiện
git addvàgit commitngay. Không đợi đến cuối ngày. -
Viết Commit Message theo Chuẩn: Tuân thủ cấu trúc Conventional Commits.
-
Tạo Pull Request (hoặc Merge Request): Đẩy branch lên và tạo Pull Request để review code.
-
Merge vào Branch chính: Sau khi được duyệt, merge vào branch chính (
develophoặcmain).
4. Ví dụ Thực tế cho Dự án Laptech
Nhiệm vụ: "Xây dựng các chức năng CRUD cho Brand". Chuỗi commit có thể như sau:
feat(brand): Add Brand entity with JPA and Lombok setupfeat(brand): Create BrandRepository interfacefeat(brand): Add DTO records and MapStruct mapper for Brandfeat(brand): Implement BrandService with CRUD business logicfeat(brand): Create BrandController with REST endpointsdocs(brand): Add Swagger documentation for Brand APIstest(brand): Add unit tests for BrandServiceImpltest(brand): Add integration tests for BrandControllerrefactor(common): Move SlugGenerator to common util packagefix(brand): Correctly handle unique slug generation in BrandService