为什么我的 git pre-commit 钩子没有运行?

whi*_*ok6 8 git husky

直到最近——我几天前才注意到这一点——我的 git pre-commit 钩子正在工作。我正在编写一个 React 应用程序,并在提交之前使用 Husky、TSLint 和 Prettier 来清理和整理我的代码。现在,当我更改和提交文件时,预提交钩子不会运行。

我的项目结构如下所示:

- project
  - .git/
  - react/   <- the frontend
    - node_modules/
    - src/
    - package.json
    - (other files)
  - nodejs/  <- the server
    - node_modules/
    - src/
    - package.json
    - (other files)
  - package.json
  - (other files)
Run Code Online (Sandbox Code Playgroud)

如果我手动执行钩子,它似乎运行良好:

[/project/react] # git status
On branch fixHusky
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   MyFile.ts

[/project/react] # ../.git/hooks/pre-commit
husky > pre-commit (node v12.6.0)
  ? Stashing changes... [skipped]
    ? No partially staged files found...
  ? Running linters...

[/project/react] # 
Run Code Online (Sandbox Code Playgroud)

但是当我真正尝试提交时,哈士奇并没有运行:

[/project/react] #  git commit -m "testing husky"
[fixHusky cf17a6b] testing husky
 1 file changed, 1 insertion(+), 1 deletion(-)

[/project/react] # 
Run Code Online (Sandbox Code Playgroud)

知道为什么它不运行吗?

whi*_*ok6 11

通过运行更新 Huskyyarn add --dev husky解决了问题。我不知道它为什么停止工作,但无论如何,哈士奇已经过时了。


Von*_*onC 6

检查git config core.hooksPath是否已设置为与其默认值不同的路径:$GIT_DIR/hooks

还要检查GIT_DIR(环境变量)当前设置。

在这两种情况下,Git会会找那个勾,您会希望(和目前有您的pre-commit钩子)