提交前钩子/pre-commit: 没有这个文件或目录。

78

当我尝试提交时出现了这个错误。

操作系统 - 最新的OSX

Git版本 - git version 2.11.0(Apple Git-81)

.git/hooks/pre-commit: line 2: ./node_modules/pre-commit/hook: No such file or directory

8
您定义了一个预提交钩子(在.git/hooks/pre-commit中),它正在尝试引用不存在的./node_modules/pre-commit/hook。如果不再需要该钩子,您可以直接将其删除。 - c3st7n
14个回答

124

在尝试提交更改时,pre-commit钩子是首先运行的,并且可以用于执行某些检查、测试和条件。在这种情况下,很明显,你没有它,这就是为什么它在抱怨。

进入.git/hooks目录并删除pre-commit文件,因为其正在尝试引用node_modules/pre-commit/hook。这样应该会解决问题。

另一种选择是在提交中添加选项:--no-verify,这将使提交时不进行检查。


我遇到了完全相同的错误,这意味着gem webpacker和webpack安装过程可能出错了;因为我刚刚做过这个。但奇怪的是pre-commit文件最后一次修改是六个月前。但移除pre-commit文件解决了我的问题。 - Greg
5
我遇到了同样的问题,但是移除 pre-commit 文件并没有解决。后来我尝试在终端中使用命令提交,并在最后加上 --no-verify 命令,所以我的命令是 git commit -m "message" --no-verify。这对我有用。 - Komal Goyani
如果设置了Lint来强制执行格式规则,就会发生这种情况。正如@ Mutant和@KomalGoyani所提到的,您可以使用git commit -n(或--no-verify)绕过Lint检查。 - Rickchip

18

您需要删除node_modules文件夹,并重新运行npm installyarn install进行安装。之后,您可能会解决问题。


9

如果脚本文件实际上存在,则可能是因为脚本的第一行应该像这样:

#!/bin/sh

否则,您将得到相同的错误。

任何文件都可以使用 - 例如,/bin/bash/usr/local/bin/python3 - 只要它存在且可执行即可。


8

根据@Mutant的提示,对我来说简单的答案是:

rm .git/hooks/pre-commit

5

我在使用macOS时遇到一个问题,我的Python3是通过Homebrew安装的。当我通过brew update && brew upgrade升级Python3后,我仍然遇到了同样的错误。

为了解决这个问题,我需要更新目录中符号链接,使得.git/hooks/pre-commit文件中的shebang (#!)指向正确的Python3路径。

以下是我在我的环境中如何解决这个问题:

  1. Look at the contents of the .git/hooks/pre-commit:

    cat .git/hooks/pre-commit
    

    The first few lines should look something like:

    #!/usr/local/opt/pre-commit/libexec/bin/python3
    # File generated by pre-commit: https://pre-commit.com
    # ID: 0123456789abcdef0123456789abcdef
    import os
    import sys
    

    Take note of the path to the python3 executable in the shebang line:

    /usr/local/opt/pre-commit/libexec/bin/
    
  2. cd into that directory.

    cd /usr/local/opt/pre-commit/libexec/bin/
    
  3. Take at look at the Python symlinks:

    ls -l | grep python
    

    You'll see some symlinks that are probably broken:

    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python -> /usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python3.9 -> /usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python3 -> /usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    

    Quick note: In case something goes wrong or my answer isn't the solution, you might want to back up these symlinks first by running:

    mv python python.bak
    
    mv python3.9 python3.9.bak
    
    mv python3 python3.bak
    
  4. Update the symbolic links using ln -s [PATH] [LINK], where [PATH] is the location of the Homebrew-updated Python3 executable and [LINK] is python, python3.9, and python3:

    ln -s /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python
    
    ln -s /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3.9
    
    ln -s /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3
    
  5. When you've done that, list your Python symlinks again.

    ls -l | grep python
    

    You should see the updated symlinks, and git commit should now work.

    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python -> /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python3 -> /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python3.9 -> /usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    

0
对于遇到 husky 问题的人,我解决了它,方法是运行:
husky install
(这是我忘记运行的)文档在“使用”下讲了一下,也就是说,在设置 husky 之后,每次运行 npm run prepare。usage

0
如果您查看package.json,您将更好地了解问题所在。 Husky是npm依赖项,因此基本上您会因为某种原因而无法使用它而出现错误。 因此,选项一运行
npm i

如果你已经做过那个的话
rm -rf node_modules
npm i

0

我在使用brew让Webstorm支持yarn时遇到了这个错误。

解决方法就是重新安装pre-commit。

brew install pre-commit

https://pre-commit.com/


0
INGW64 ~/code/ingestor-gdelt (test-build)
$ git commit -m "your comment"

.git/hooks/pre-commit: 第9行:py:命令未找到

.git/hooks/pre-commit: 第10行:py:命令未找到

git commit --no-verify -m "your comment"

这个命令解决了我的问题!!


3
它暂时解决了问题,但也跳过了您的预提交任务,并没有真正解决问题。这只是一个绕过错误的解决方法。 - drecunion

0
我使用homebrew安装了pre-commit,并安装了pre-commit-vscode扩展,但出现了错误。
解决方法是在git仓库的根目录下运行pre-commit install

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