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

9

最近我注意到了一个问题,我的git pre-commit hook不起作用了。我正在编写一个React应用程序,使用Husky、TSLint和Prettier在提交之前清理和lint我的代码。现在,当我修改并提交文件时,pre-commit hook不会运行。

我的项目结构如下:

- 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)

如果我手动执行这个钩子,它似乎可以正常运行:
[/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] # 

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

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

[/project/react] # 

不知道为什么它没运行?


为什么使用 ../.git/hooks/pre-commit 而不是 ./.git/hooks/pre-commit?也许你有两个 .git 目录(../.git./.git)? - aropan
你在 package.json 文件中加入钩子了吗? - evolutionxbox
我使用了 ../ 而不是 ./,因为我们的项目有两个目录,一个用于服务器,另一个用于React。Git仓库位于根目录,但是linting应该只在React文件夹中进行。我会编辑问题以使其更清晰明了。 - whiterook6
2个回答

12

通过运行yarn add --dev husky来更新husky解决了问题。我不知道为什么它停止工作,但是husky已经非常过时了。


6

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

同时,请确保 GIT_DIR (环境变量)目前未设置。

无论哪种情况,Git 都会在你预期以外的位置寻找该钩子(而不是你当前的 pre-commit 钩子所在位置)。


看起来两者都是空的。GIT_DIR 应该设置为什么?没有它,我尝试手动设置 hooksPath:git config core.hooksPath "hooks/*" 但是没有用。 - whiterook6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接