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

时间:2019-07-17 22:10:01

标签: 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)

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

[/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] # 

但是当我实际尝试提交时,赫斯基无法运行:

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

[/project/react] # 

知道为什么它不运行吗?

2 个答案:

答案 0 :(得分:1)

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

还要检查GIT_DIR(环境变量)当前是否设置为

在两种情况下,Git都会在您期望的位置找到钩子(em>不是(当前具有您的预提交钩子)

答案 1 :(得分:1)

通过运行yarn add --dev husky更新Husky可以解决此问题。我不知道为什么它停止工作,但是无论如何,沙哑的已经过时了。