28
Git Commit 规范与 Emoji
Git Commit 规范与 Emoji
为什么需要 Commit 规范?
好的 Commit 信息能够:
- 快速理解每次变更的目的
- 自动生成 Changelog
- 方便代码审查和回溯
- 让团队协作更高效
Commit Message Format
type(scope): description
[optional body]
[optional footer]
Commit Types with Emoji
| Emoji | Type | Description |
|---|---|---|
| ✨ | feat | New features |
| 🐛 | fix | Bug fixes |
| 📝 | docs | Documentation changes |
| ♻️ | refactor | Code restructuring without changing functionality |
| 🎨 | style | Code formatting, missing semicolons, etc. |
| ⚡️ | perf | Performance improvements |
| ✅ | test | Adding or correcting tests |
| 🧑💻 | chore | Tooling, configuration, maintenance |
| 🚧 | wip | Work in progress |
| 🔥 | remove | Removing code or files |
| 🚑 | hotfix | Critical fixes |
| 🔒 | security | Security improvements |
Commit Process
- Check for staged changes (
git status) - If no staged changes, review and stage appropriate files
- Run pre-commit checks (unless --no-verify)
- Analyze changes to determine commit type
- Generate descriptive commit message
- Include scope if applicable:
type(scope): description - Add body for complex changes explaining why
- Execute commit
Best Practices
- Keep commits atomic and focused
- Write in imperative mood ("Add feature" not "Added feature")
- Explain why, not just what
- Reference issues/PRs when relevant
- Split unrelated changes into separate commits
Examples
Feature Addition
git commit -m "✨ feat(auth): add OAuth2 login support
- Implemented Google OAuth2 provider
- Added token refresh mechanism
- Updated user session handling
Closes #123"
Bug Fix
git commit -m "🐛 fix(api): handle null response in user endpoint
The API was crashing when user data was incomplete.
Added null checks and default values.
Fixes #456"
Documentation
git commit -m "📝 docs: update API documentation for v2 endpoints"
Refactoring
git commit -m "♻️ refactor(utils): extract validation logic to separate module"
Fast Commit Workflow
如果你想快速提交,可以使用这个流程:
- Stage all changes:
git add . - Generate 3 commit message suggestions
- Auto-select the first one
这在你确定改动很清晰的情况下非常有用。
Pre-commit Checks
建议在提交前运行:
- Linting (ESLint, Prettier)
- Type checking (TypeScript)
- Unit tests
可以使用 husky 配置 pre-commit hooks 自动运行这些检查。
下一步
学习 五个为什么分析法 来深入分析问题根因。