摘要:最近公司对 Git 提交要求越来越严格,一条 message 写错,绩效就“哐哐哐的”扣写错 git commit = 绩效掉血 commitlint + husky 上阵,报错就改,绩效和心脏都稳稳的
最近公司对 Git 提交要求越来越严格,一条 message 写错,绩效就“哐哐哐的”扣
写错 git commit = 绩效掉血 commitlint + husky 上阵,报错就改,绩效和心脏都稳稳的
创建 init-husky.ps1 文件:
Write-Host "开始初始化 commitlint + husky ..." -ForegroundColor Cyanif (-not (Test-Path "package.json")) { Write-Host "初始化 npm 项目..." -ForegroundColor Green npm init -y } else { Write-Host "已存在 package.json,跳过 npm init" -ForegroundColor Yellow }Write-Host "安装依赖..." -ForegroundColor #技术分享 Green npm install --save-dev husky @commitlint/cli @commitlint/config-conventionalWrite-Host "初始化 husky..." -ForegroundColor Green npx husky initWrite-Host "创建 commitlint.config.js ..." -ForegroundColor Green $commitlintConfig = @" module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'type-enum': [ 2, 'always', ['feat','fix','refactor','docs','style','chore','perf','test','build','ci','revert'] ], 'subject-case': [0] } }; "@ Set-Content -Path commitlint.config.js -Value $commitlintConfig -Encoding UTF8Write-Host "初始化完成" -ForegroundColor Cyan Write-Host "你现在可以使用规范的 commit 提交了,例如:" -ForegroundColor Yellow Write-Host 'git commit -m "feat(login): 新增用户搜索功能"' -ForegroundColor Yellow# 使用说明Write-Host "`n PowerShell 执行脚本权限:" -ForegroundColor Cyan Write-Host "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass" -ForegroundColor Yellow Write-Host "执行脚本:" -ForegroundColor Cyan Write-Host ".\init-husky.ps1" -ForegroundColor Yellow执行:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass.\init-husky.ps1创建 init-husky.sh 文件:
#!/bin/bashecho "开始初始化 commitlint + husky ..."if [ ! -f "package.json" ]; then echo "初始化 npm 项目..." npm init -y else echo "已存在 package.json,跳过 npm init" fiecho "安装依赖..." npm install --save-dev husky @commitlint/cli @commitlint/config-conventionalecho "初始化 husky..." npx husky installecho "创建 commitlint.config.js ..." cat > commitlint.config.js执行:
chmod +x init-husky.sh./init-husky.shtype :必须小写,常用类型:| Type | 说明 | | ---
| feat | 新增功能 | | fix | 修复 bug | | docs | 修改文档 | | style | 代码格式/排版调整(不影响逻辑) | | refactor | 重构代码(不改功能) | | perf | 性能优化 | | test | 增加/修改测试用例 | | build | 构建或依赖修改 | | ci | 持续集成配置 | | chore | 杂项改动/维护性任务 | | revert | 回滚之前某次提交 |
scope :可选,说明影响模块/组件/功能区subject :简要说明,使用祈使句,首字母小写,不要句号,50 字以内Why > What :标题说明“做了什么”,详细原因放 Body(标题行) │ │ │ │ │ └─ subject(主题) -│ └─ scope(范围) -└─ type(类型) - feat/fix/docs/style/refactor/perf/test/build/ci/chore/revert(正文) 解释动机、背景、解决方案、副作用等 每行建议 ≤72字符,可分段(页脚) 说明破坏性变更或关联 Issue# Header + Body + Footergit commit -m "feat(login): 新增用户搜索功能" \ -m "增加搜索接口调用,优化性能,支持按用户名和邮箱搜索" \ -m "Closes #123"# 只写 Headergit commit -m "feat(login): 新增用户搜索功能"| Type | Scope (示例) | 说明 | | ---
| feat | login, search, profile | 新增功能模块 | | fix | api, ui, auth | 修复 bug | | docs | README, CHANGELOG | 文档修改 | | style | code, formatting | 代码格式/排版调整 | | refactor | models, utils | 重构代码 | | perf | search, database | 性能优化 | | test | login, api | 增加/修改测试用例 | | build | webpack, npm | 构建或依赖修改 | | ci | travis, github-actions | 持续集成配置 | | chore | scripts, tools | 杂项改动/维护性任务 | | revert | commit SHA | 回滚之前某次提交 |
git commit -m "feat(search): 新增用户搜索功能"[main 1a2b3c4] feat(search): 新增用户搜索功能 2 files changed, 35 insertions(+)小技巧:
VSCode 等编辑器提交也会自动触发 Husky hook
提交失败时,按提示修改 message 即可
@1.0.0 test > echo "Error: no test specified" && exit 1Error: no test specifiedhusky - pre-commit script failed (code 1)原因
默认 npm test 未配置,Husky pre-commit 会执行 npm test ,导致报错。解决方案
方案 A:给 test 脚本一个空实现(最简单)
"scripts": { "test": "exit 0"}方案 B:修改 pre-commit hook,不执行 test
#!/bin/sh. "$(dirname "$0")/_/husky.sh"exit 0错误 2️⃣:PowerShell 写 hook 文件 LF/CRLF 问题原因
PowerShell 默认编码和换行符可能和 Git / Husky 不兼容解决方案
不要用 Set-Content 写 hook 文件推荐编辑器直接修改,或用:npx husky add .husky/pre-commit "npx lint-staged"git commit -m '测试一下提交'✖ subject may not be empty [subject-empty]✖ type may not be empty [type-empty]解决方案
git commit -m "feat(login): 新增用户搜索功能"来源:墨码行者