最近公司对 Git 提交要求越来越严格,一条 message 写错,绩效就"哐哐哐的"扣 😅 写错 git commit = 绩效掉血 💔 commitlint + husky 上阵,报错就改,绩效和心脏都稳稳的 😏
1️⃣ Windows (PowerShell) 初始化
创建 init-husky.ps1
文件:
perl
Write-Host "开始初始化 commitlint + husky ..." -ForegroundColor Cyan
# 1️⃣ 初始化 npm(如果已有 package.json 可跳过)
if (-not (Test-Path "package.json")) {
Write-Host "初始化 npm 项目..." -ForegroundColor Green
npm init -y
} else {
Write-Host "已存在 package.json,跳过 npm init" -ForegroundColor Yellow
}
# 2️⃣ 安装依赖
Write-Host "安装依赖..." -ForegroundColor Green
npm install --save-dev husky @commitlint/cli @commitlint/config-conventional
# 3️⃣ 初始化 husky
Write-Host "初始化 husky..." -ForegroundColor Green
npx husky init
# 4️⃣ 创建 commitlint 配置文件(支持完整规范 type)
Write-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 UTF8
Write-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
执行:
sql
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\init-husky.ps1
2️⃣ Mac / Linux (Bash) 初始化
创建 init-husky.sh
文件:
bash
#!/bin/bash
echo "开始初始化 commitlint + husky ..."
# 1️⃣ 初始化 npm(如果已有 package.json 可跳过)
if [ ! -f "package.json" ]; then
echo "初始化 npm 项目..."
npm init -y
else
echo "已存在 package.json,跳过 npm init"
fi
# 2️⃣ 安装依赖
echo "安装依赖..."
npm install --save-dev husky @commitlint/cli @commitlint/config-conventional
# 3️⃣ 初始化 husky
echo "初始化 husky..."
npx husky install
# 4️⃣ 创建 commitlint 配置文件(支持完整规范 type)
echo "创建 commitlint.config.js ..."
cat > commitlint.config.js <<EOF
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]
}
};
EOF
echo "初始化完成"
echo "你现在可以使用规范的 commit 提交了,例如:"
echo 'git commit -m "feat(login): 新增用户搜索功能"'
# 使用说明
echo
echo "💡 脚本执行权限(Mac/Linux 默认可执行):"
echo "chmod +x ./init-husky.sh"
echo "执行脚本:"
echo "./init-husky.sh"
执行:
bash
chmod +x init-husky.sh
./init-husky.sh
3️⃣ Git Commit Message 规范
标题行 (Header) - 必须
xml
<type>(<scope>): <subject>
- type:必须小写,常用类型:
Type | 说明 |
---|---|
feat | 新增功能 |
fix | 修复 bug |
docs | 修改文档 |
style | 代码格式/排版调整(不影响逻辑) |
refactor | 重构代码(不改功能) |
perf | 性能优化 |
test | 增加/修改测试用例 |
build | 构建或依赖修改 |
ci | 持续集成配置 |
chore | 杂项改动/维护性任务 |
revert | 回滚之前某次提交 |
- scope:可选,说明影响模块/组件/功能区
- subject:简要说明,使用祈使句,首字母小写,不要句号,50 字以内
- Why > What:标题说明"做了什么",详细原因放 Body
正文 (Body) - 可选
- 描述动机、背景、解决方案、副作用
- 每行建议 72 字左右
- 使用祈使句,可分段落
页脚 (Footer) - 可选
- 破坏性变更(Breaking Changes)
- 关闭 Issue,例如
Closes #123
4️⃣ Git Commit 结构示意图
markdown
<Header>(标题行) - 必须
----------------------------------------------------------
feat(login): 新增用户搜索功能
│ │ │
│ │ └─ subject(主题) - 简要描述本次变更
│ └─ scope(范围) - 可选,影响模块/组件/功能区
└─ type(类型) - feat/fix/docs/style/refactor/perf/test/build/ci/chore/revert
<Body>(正文) - 可选
----------------------------------------------------------
增加搜索接口调用,优化性能,支持按用户名和邮箱搜索
解释动机、背景、解决方案、副作用等
每行建议 ≤72字符,可分段
<Footer>(页脚) - 可选
----------------------------------------------------------
Closes #123
说明破坏性变更或关联 Issue
5️⃣ Git Commit 示例
css
# Header + Body + Footer
git commit -m "feat(login): 新增用户搜索功能" \
-m "增加搜索接口调用,优化性能,支持按用户名和邮箱搜索" \
-m "Closes #123"
# 只写 Header
git commit -m "feat(login): 新增用户搜索功能"
6️⃣ 推荐常用 Type & Scope 模板
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 | 回滚之前某次提交 |
7️⃣ commitlint + husky 可视化示例
✅ 正确提交
sql
git commit -m "feat(search): 新增用户搜索功能"
[main 1a2b3c4] feat(search): 新增用户搜索功能
2 files changed, 35 insertions(+)
❌ 错误提交
sql
PS D:\work\Android\as_workspace_git\project> git commit -m '测试一下提交'
⧗ input: 测试一下提交
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
husky - pre-commit script failed (code 1)
💡 小技巧:
- VSCode 等编辑器提交也会自动触发 Husky hook
- 提交失败时,按提示修改 message 即可
8️⃣ 常见错误及解决方案
错误 1️⃣:husky - pre-commit script failed (code 1)
bash
@1.0.0 test > echo "Error: no test specified" && exit 1
Error: no test specified
husky - pre-commit script failed (code 1)
原因
- 默认
npm test
未配置,Husky pre-commit 会执行npm test
,导致报错。
解决方案
方案 A:给 test 脚本一个空实现(最简单)
json
"scripts": {
"test": "exit 0"
}
方案 B:修改 pre-commit hook,不执行 test
bash
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# 如果你有 lint-staged,可以写:
# npx lint-staged
# 没有测试或 lint 就直接退出
exit 0
- commitlint 只在 commit-msg hook 执行
exit 0
表示 hook 成功,不阻止 commit
错误 2️⃣:PowerShell 写 hook 文件 LF/CRLF 问题
原因
- PowerShell 默认编码和换行符可能和 Git / Husky 不兼容
解决方案
- 不要用
Set-Content
写 hook 文件 - 推荐编辑器直接修改,或用:
sql
npx husky add .husky/pre-commit "npx lint-staged"
错误 3️⃣:提交信息格式不规范
sql
git commit -m '测试一下提交'
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
解决方案
- 按规范提交:
scss
git commit -m "feat(login): 新增用户搜索功能"
- 规范格式:
xml
<type>(<scope>): <subject>
错误 4️⃣:VSCode 提交提示失败
- VSCode 内置 Git 会自动触发 Husky hook
- 提交失败时按提示修改 message 或 fix 代码即可
9️⃣ 小结
- 重复执行安全:已有 package.json、husky、commitlint 配置或 hook 会自动跳过
- 脚本化管理 :无论 Windows 还是 Mac/Linux,使用
.ps1
或.sh
脚本初始化最稳妥 - 规范提交:保证提交信息清晰,绩效稳稳的