☠️ 写错 commit = 绩效自爆,别说我没提醒你!

最近公司对 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 脚本初始化最稳妥
  • 规范提交:保证提交信息清晰,绩效稳稳的
相关推荐
自动花钱机1 小时前
Cherry-pick冲突与Git回滚
git
coderklaus7 小时前
git rebase
git
程序设计实验室7 小时前
模型文件硬塞进 Git,GitHub 直接打回原形:使用Git-LFS管理大文件
git
Dontla1 天前
脚本:git push直到成功(windows powershell命令)(Github连不上、Github断开)
git·github
CAE虚拟与现实1 天前
GitHub Desktop 和 Git 命令行工具(CLI)各有优势
git·github·github desktop
RePeaT1 天前
代码双仓库备份指南:三种简单高效的方法
git·github
coderklaus1 天前
Git GC
git
xiezhr1 天前
Git提交错了,别慌!还有后悔药
git·gitlab·github
GGGGGGGGGGGGGG.1 天前
CI/CD 全链路实践:从 Git 基础到 Jenkins + GitLab 企业级部署
运维·git·ci/cd·云原生·gitlab·jenkins