/bin/sh: validate-commit-msg: command not found 的报错

小明 2025-05-06 14:14:09 8

这个错误通常���味着你的 Git 钩子(hook)尝试执行 `validate-commit-msg` 脚本来校验 commit 信息的格式,但是无法在系统上找到这个命令。这可能是因为 `validate-commit-msg` 没有被正确安装在你的项目中,或者是因为项目配置指向了一个不再存在的校验工具。

()

### 解决步骤

#### 方法 1: 安装或重新安装 `commitlint`

()

由于 `validate-commit-msg` 已被废弃,推荐使用 `commitlint`。以下是使用 `commitlint` 的步骤:

1. **安装 `commitlint` 及其配置包**

```bash

npm install --save-dev @commitlint/{config-conventional,cli}

```

2. **配置 `commitlint`**

在你的项目根目录下创建一个 `commitlint.config.js` 配置文件,并添加以下内容:

```javascript

module.exports = {extends: ['@commitlint/config-conventional']};

```

3. **设置 Git 钩子来使用 `commitlint`**

安装 `husky` 来管理 Git 钩子:

```bash

npm install husky --save-dev

```

在 `package.json` 中配置 `husky` 来使用 `commitlint`:

```json

"husky": {

  "hooks": {

    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"

  }

}

```

确保这些更改后,尝试再次提交,看是否解决了问题。

#### 方法 2: 移除旧的 Git 钩子

如果你不需要 `validate-commit-msg` 或任何提交消息校验,你可以移除或禁用相关的 Git 钩子。

1. **查找并编辑或删除 Git 钩子**

Git 钩子位于你的 Git 仓库的 `.git/hooks/` 目录中。找到 `commit-msg` 钩子:

```bash

cd .git/hooks/

ls

```

如果有 `commit-msg` 文件,你可以编辑或删除它:

- **编辑**: 使用文本编辑器打开 `commit-msg` 文件,注释掉调用 `validate-commit-msg` 的行。

- **删除**: 直接删除 `commit-msg` 文件。

```bash

rm commit-msg

```

2. **测试更改**

在做出更改后,尝试再次提交以确保问题已解决。

### 注意

- 在做任何更改前,确认是否在团队内部有统一的代码提交规范和工具链要求。

- 如果你是在使用某个特定的项目模板或脚手架工具,查看其文档,因为可能包含有关如何正确配置提交消息校验的指南。

The End
微信