Git 添加预提交挂钩而不是提交的暂存文件

Git add in pre-commit hook not staging file for commit

我已经编写了一个预提交挂钩来编译我的项目并将生成的文件添加到提交中。

这是一个 JavaScript 项目,我正在使用 husky,但我也尝试过编辑 .git/hooks/pre-commit,但该文件没有添加到提交中。如果我取消提交,我可以看到文件已添加,但由于某种原因,这不适用于当前提交。

我的预提交挂钩类似于:

const shell = require('shelljs');

shell.exec('yarn bundle');
shell.exec('git add dist');
shell.exit(0);

shelljs 只是一个在 node

中执行交叉 OS unix 命令的库

我将 .git/hooks/pre-commit 编辑为 运行 git add dist,但文件仍未添加到提交中

我不认为 git add 可以在 pre-commit hook 中工作,用于检查将要提交的内容,而不是修改它.

您可以改用类似于“Can a Git hook automatically add files to the commit?”的方法,这会创建一个单独的附加提交。