Quy ước Git
Module 10.2: Quy ước Git
Phần tiêu đề “Module 10.2: Quy ước Git”Thời gian học: ~30 phút
Yêu cầu trước: Module 10.1 (CLAUDE.md cho Team)
Kết quả: Sau module này, bạn sẽ có git convention work cho AI-assisted development, biết train Claude follow chúng, và hiểu best practice cho commit attribution.
1. WHY — Tại Sao Cần Hiểu
Phần tiêu đề “1. WHY — Tại Sao Cần Hiểu”Dev dùng Claude implement feature. Claude commit 1 cục với message “implemented feature.” PR reviewer không hiểu change gì. Dev khác thì Claude commit verbose như tiểu thuyết. Git history vô dụng cho debug.
Git convention tồn tại cho human. Khi AI generate commit, convention quan trọng HƠN — vì AI consistently follow (hoặc consistently ignore). Module này làm Claude trở thành good git citizen.
2. CONCEPT — Ý Tưởng Cốt Lõi
Phần tiêu đề “2. CONCEPT — Ý Tưởng Cốt Lõi”Commit Message Convention
Phần tiêu đề “Commit Message Convention”<type>(<scope>): <short description>
[optional body: what and why]
🤖 Generated with Claude CodeTypes: feat, fix, refactor, docs, test, chore
Commit Granularity Rules
Phần tiêu đề “Commit Granularity Rules”| Rule | Good | Bad |
|---|---|---|
| Atomic commit | One logical change per commit | ”implemented everything” |
| Reviewable size | 50-200 lines per commit | 2000-line commit |
| Builds pass | Each commit compile/pass test | Broken intermediate commit |
Branch Naming
Phần tiêu đề “Branch Naming”<type>/<ticket>-<short-description>
feat/PROJ-123-user-authfix/PROJ-456-login-validationrefactor/PROJ-789-payment-serviceAI Attribution
Phần tiêu đề “AI Attribution”- Footer marker:
🤖 AI-assistedtrong commit message (recommended) - Co-author:
Co-authored-by: Claude <noreply@anthropic.com> - Branch suffix:
-ai-assisted - PR label: Add “AI-assisted” label
Footer marker là cách ít intrusive nhất, informative nhất.
3. DEMO — Từng Bước
Phần tiêu đề “3. DEMO — Từng Bước”Scenario: Team adopt Conventional Commits với AI attribution.
Step 1: Add Git Convention vào CLAUDE.md
Phần tiêu đề “Step 1: Add Git Convention vào CLAUDE.md”## Git Conventions
### Commit Message Format<type>(<scope>): <short description>
[body: explain what and why, not how]
🤖 Generated with Claude Code
### Types- feat: New feature- fix: Bug fix- refactor: Code change không fix cũng không add- docs: Documentation only- test: Adding test- chore: Maintenance
### Rules- Subject line: max 50 character, imperative mood- Body: wrap at 72 character- One logical change per commit- Each commit phải pass: npm test && npm run lintStep 2: Train Claude với Example
Phần tiêu đề “Step 2: Train Claude với Example”Bạn: Tôi đã add git convention vào CLAUDE.md. Practice thử.Tôi vừa add input validation vào login form.Generate commit theo convention.
Claude: Đây là commit theo convention của team:
git commit -m "feat(auth): add input validation to login form
Add email format validationAdd password strength requirementsDisplay inline error messages
🤖 Generated with Claude Code"Step 3: Multi-Commit Workflow
Phần tiêu đề “Step 3: Multi-Commit Workflow”Bạn: Implement user registration với email verification.Break thành atomic commit theo granularity rule.
Claude: Tôi sẽ implement trong 4 atomic commit:1. feat(auth): add user registration endpoint2. feat(auth): add email verification token generation3. feat(auth): add verification email sending4. feat(auth): add email verification confirmation endpoint
Mỗi commit independently testable. Proceed?Step 4: Verify trong Git Log
Phần tiêu đề “Step 4: Verify trong Git Log”$ git log --oneline -5Output:
abc1234 feat(auth): add email verification confirmation endpointdef5678 feat(auth): add verification email sendingghi9012 feat(auth): add email verification token generationjkl3456 feat(auth): add user registration endpointmno7890 docs: update CLAUDE.md with git conventions4. PRACTICE — Tự Thực Hành
Phần tiêu đề “4. PRACTICE — Tự Thực Hành”Bài 1: Convention Setup
Phần tiêu đề “Bài 1: Convention Setup”Goal: Configure git convention cho team.
Instructions:
- Add git convention section vào CLAUDE.md
- Include: message format, types, granularity rule
- Include AI attribution approach
- Test với small change
💡 Hint
## Git Conventions- Format: type(scope): description- Types: feat, fix, refactor, docs, test, chore- Max 50 char subject line- Add 🤖 AI-assisted footerBài 2: Atomic Commit Drill
Phần tiêu đề “Bài 2: Atomic Commit Drill”Goal: Practice proper commit granularity.
Instructions:
- Ask Claude implement medium feature
- Require break thành 3-5 atomic commit
- Review each commit: Truly atomic? Pass test alone?
- Iterate CLAUDE.md nếu Claude’s granularity sai
Bài 3: History Readability
Phần tiêu đề “Bài 3: History Readability”Goal: Validate convention work.
Instructions:
- Sau 1 ngày dùng Claude, run
git log --online -20 - Có thể hiểu chuyện gì xảy ra từ message alone?
- Nếu không, thiếu gì? Update CLAUDE.md.
✅ Solution
Good git log:
abc1234 feat(cart): add quantity validationdef5678 fix(cart): handle empty cart checkoutghi9012 refactor(cart): extract price calculationjkl3456 test(cart): add unit tests for CartServiceBad git log:
abc1234 updatesdef5678 WIPghi9012 fixed stuffjkl3456 implemented featureNếu log giống bad example, add rule cụ thể hơn vào CLAUDE.md.
5. CHEAT SHEET
Phần tiêu đề “5. CHEAT SHEET”Commit Message Format
Phần tiêu đề “Commit Message Format”<type>(<scope>): <description>
[body]
🤖 Generated with Claude CodeTypes
Phần tiêu đề “Types”feat | fix | refactor | docs | test | chore
Granularity Rules
Phần tiêu đề “Granularity Rules”- One logical change per commit
- 50-200 lines per commit
- Each commit pass test
Branch Naming
Phần tiêu đề “Branch Naming”<type>/<ticket>-<description>feat/PROJ-123-user-authCLAUDE.md Git Section
Phần tiêu đề “CLAUDE.md Git Section”## Git Conventions- Format: type(scope): description- Max 50 char subject- Add 🤖 AI-assisted footer- Atomic commit, each pass testQuick Prompt
Phần tiêu đề “Quick Prompt”- “Commit theo convention của team”
- “Break thành atomic commit”
- “Commit type phù hợp cho change này?“
6. PITFALLS — Lỗi Thường Gặp
Phần tiêu đề “6. PITFALLS — Lỗi Thường Gặp”| ❌ Sai Lầm | ✅ Đúng Cách |
|---|---|
| ”Just commit” không format | Explicit format trong CLAUDE.md |
| Giant commit (“implemented feature”) | Require atomic commit |
| Không AI attribution | Footer marker: 🤖 AI-assisted |
| Commit broken code | Rule: each commit pass test |
| Message vague | Require body explain why |
| Inconsistent giữa team member | Same CLAUDE.md = same convention |
| Obsess perfect commit | Good enough OK. Squash trong PR nếu cần. |
7. REAL CASE — Câu Chuyện Thực Tế
Phần tiêu đề “7. REAL CASE — Câu Chuyện Thực Tế”Scenario: Team Việt Nam, 8 developer, heavy Claude Code usage. Git history unusable:
- “fixed stuff”
- “WIP”
- 500-line commit không description
- Không thể hiểu feature evolution
Solution: Git convention trong CLAUDE.md
Added:
- Conventional Commits format
- 🤖 footer cho AI-generated code
- Atomic commit requirement
- Pre-commit hook: reject commit không đúng format
Result sau 2 tuần:
git logreadable- Code review time: giảm 30% (reviewer hiểu commit intent)
- Debug easier (có thể bisect với meaningful commit)
- New dev hiểu history không cần hỏi
Unexpected benefit: AI attribution giúp identify pattern — “AI-generated commit có fewer bug nhưng sometimes miss edge case” — trở thành learning cho team.
Tiếp theo: Module 10.3: Quy trình Code Review →