尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

【git】约定式提交规范

【git】约定式提交规范 1. 背景与问题描述Git 提交信息的“混沌时代”我们在代码评审中是否经常看到这样的提交信息git commit -m update git commit -m 修改了bug git commit -m fix something git commit -m .如果你曾经因为git log输出毫无意义而抓狂或者在v1.0.0与v1.0.1之间翻遍了修改记录却无从知晓版本差异——那么你正深陷Git 提交信息的混沌时代。更致命的是无法自动化生成 CHANGELOG手动整理变更日志耗时且易漏无法语义化版本发布不知道该升 minor 还是 patch无法精准定位回归 bug找不到引入问题的那一行逻辑协作低效队友无法快速理解一个 commit 的意图。根因很简单提交信息没有格式约束每个人都在自由发挥。而工业界的答案早在 2014 年就已诞生——约定式提交规范Conventional Commits由 Angular 团队率先确立随后被 Google、Facebook、TypeScript、Vue 等全球顶级项目奉为圭臬。2. 核心原理解析一条格式化字符串如何撬动整个工程体系2.1 语法结构极简公式约定式提交的提交信息格式如下type(scope): description真实示例feat(user-auth): add JWT token refresh logic fix(ui-table): resolve column misalignment when sorting perf(core): reduce bundle size by lazy-loading routes2.2 常见 type 语义词典Type含义是否需提高版本号feat新功能是 (minor)fix修复 bug是 (patch)perf性能优化是 (patch)refactor重构不改变外部行为否chore构建、工具链、依赖更新等杂务否docs仅文档修改否test增加或修改测试否style代码风格调整无逻辑变化否build构建系统或外部依赖变更否ciCI 配置变更否2.3 为什么说这能规避“AI 痕迹”我注意到很多开发者的 GitHub 提交信息一眼就能被识别为“机器生成”或“新手写”因为信息毫无结构、缺乏语义层级。而约定式提交体现的是——你的提交信息是可推理的代码评审者能立即判断这个提交改变了什么性质新增/修复/重构机器能解析 commit 信息自动生成 CHANGELOG团队能在 release 分支上精准区分 breaking change 与普通提交。这不仅仅是规范更是工程素养的视觉化呈现。3. 实战/代码演示从零接入约定式提交3.1 手动使用规范提交格式先从最朴素的手写方式开始# 新功能作用于 user 模块gitcommit-mfeat(user): add phone verification endpoint# 修复 bug作用于 cart 模块gitcommit-mfix(cart): prevent double-submission on checkout完整示例带 bodygitcommit-mfix(auth): refresh token now rotates on every request The previous implementation did not rotate refresh tokens, leading to a potential security risk where a stolen token remains valid indefinitely. Closes #2343.2 工具链加持Commitizen Husky lint-staged手动记忆规则容易出错我们使用现代前端工程标配工具来强化规范。Step 1: 安装依赖npminstall--save-dev commitlint/cli commitlint/config-conventionalnpminstall--save-dev huskynpminstall--save-dev commitizennpminstall--save-dev cz-conventional-changelogStep 2: 配置 commitlint强制校验 commit 信息创建commitlint.config.jsmodule.exports{extends:[commitlint/config-conventional],rules:{type-enum:[2,always,[feat,fix,perf,refactor,chore,docs,test,style,build,ci,revert]],subject-case:[0]// 关闭主题大小写强制}};Step 3: 配置 Husky提交前钩子在package.json中{husky:{hooks:{commit-msg:commitlint -E HUSKY_GIT_PARAMS}}}执行npx huskyinstallnpx huskyadd.husky/commit-msgnpx --no-install commitlint --edit$1Step 4: 配置 Commitizen交互式生成规范提交在package.json中添加{scripts:{commit:git cz},config:{commitizen:{path:./node_modules/cz-conventional-changelog}}}现在运行npm run commit会弹出友好的交互式提示? Select the type of change youre committing: (Use arrow keys) ❯ feat: A new feature fix: A bug fix docs: Documentation only changes refactor: A code change that neither fixes a bug nor adds a feature perf: A code change that improves performance test: Adding missing tests or correcting existing tests ...Step 5: 自动生成 CHANGELOG安装standard-versionnpminstall--save-dev standard-version在package.json中添加脚本{scripts:{release:standard-version}}然后当你发布新版本时只需运行npmrun release它会自动根据 commit 信息确定版本号feat → minorfix → patch自动生成 CHANGELOG.md自动打 git tag。3.3 实战演示一个完整的 CI 流程# .github/workflows/release.ymlname:Releaseon:push:branches:[main]jobs:release:runs-on:ubuntu-lateststeps:-uses:actions/checkoutv3-uses:actions/setup-nodev3with:node-version:18-run:npm ci-name:Conventional Commits Checkuses:amannn/action-semantic-pull-requestv5env:GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}}-name:Releaserun:npx semantic-release4. 总结与避坑指南4.1 为什么不建议跳过“body”正文约定式提交不仅要求标题规范重要提交应包含 body 说明解释“为什么”做这个修改而不只是“做了什么”提及 breaking change 时使用BREAKING CHANGE:前缀触发 major 版本升级。示例fix!: remove deprecated user list API BREAKING CHANGE: the /api/users/list endpoint is removed. Use /api/users with pagination instead.4.2 常见避坑要点踩坑经验谈坑点正确做法type 拼写错误如fixs严格遵守 type 枚举用 commitlint 强校验scope 滥用写太长scope 保持简短如ui、api、dbdescription 写英文但全小写规范要求subject-case: [0]需自定义推荐首字母大写或全小写一致多个功能一次性提交一个 commit 只做一件事可读性远胜一切忘了 breaking change 标记如果破坏了向后兼容务必在 body 写BREAKING CHANGE:不使用工具纯手工手工容易出错务必接上 Husky commitlint4.3 最终寄语约定式提交规范的意义表面上是一套格式实际上是一种团队契约——它让代码历史变得可读、可审计、可信任。当你的 git log --oneline 输出变为 f87a2b9 feat(auth): implement OAuth2 PKCE flow 4c21a8d fix(cart): correct tax calculation for promo codes b77e2c1 perf(images): enable WebP compression on CDN 09d0faf refactor(api): extract validation middleware 8a111cc chore(deps): upgrade lodash to 4.17.21 那一刻你的提交信息已经不再只是文字——而是你作为工程师的职业名片。从今天起告别update和fix something。让每一次提交都展现出你的工程素养。
返回列表