【已解决】使用 husky、commitlint 后 git commit 报错:No staged files match any configured task.


一、问题

git commit 报错:No staged files match any configured task.

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[34m→[39m No staged files match any configured task.
⧗   input: feat: XXX
✖   subject may not be empty [subject-empty]type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky - commit-msg hook exited with code 1 (error)

二、解决

这就涉及到 git hooks 了,熟悉的小伙伴自不用多说,不熟悉的可以看一下官方文档:Git - Git 钩子

如果报错中有 huskycommitlint 这样的关键字眼,那就是前端项目中使用了这两种 npm 插件,husky 的配置文件在如下截图位置:

在这里插入图片描述

  • 最粗暴的解决方式是使用 # 注释掉最后一行(不建议,急用临时提交可行)
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}

若是使用 JetBrains公司的系列产品时(WebStrom / PhpStrom / IDEA …),可以:去掉勾选(不限前端项目)

在这里插入图片描述

  • 检查 package.jsonnode_modules 该依赖是否安装:
{
    
    
  "devDependencies": {
    
    
    "@commitlint/cli": "^17.4.4",
    "@commitlint/config-conventional": "^17.4.4",
    "husky": "^8.0.3",
  },
}

我这里最终发现问题就出现在这里( package.json 在某次代码合并中被不知名小可爱覆盖。。。刚好缺了 commitlint

若是内网开发,规范可见源码:

  • node_modules\@commitlint\config-conventional\index.js
  • 检查所有配置之后,确定没问题的话,那么删除 node_modules ,然后重新 npm i 试试

over


三、拓展学习

猜你喜欢

转载自blog.csdn.net/qq_32682301/article/details/129286285