Jitendra Vyas
Jitendra Vyas

Reputation: 152817

pre-commit/hook: No such file or directory

I get this error when I try to commit.

OS - Latest OSX

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

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

Upvotes: 86

Views: 174179

Answers (14)

Viki
Viki

Reputation: 117

In my case in Mac OS, upgrading python3 via brew somehow messed up python3 location whereas the shebang in pre-commit was was still pointing to the old python3 location. So finding new python3 location via which python3 and updating same in pre-commit and other scripts under .githooks folder did fixed the issue.

Upvotes: 0

Stian
Stian

Reputation: 1391

I had installed pre-commit using homebrew, and installed the pre-commit-vscode extension and got this error.

What solved this was to also run pre-commit install from the root of the git repo.

Upvotes: 0

Ricardo Barroso
Ricardo Barroso

Reputation: 763

Following @Mutant tip, the simple answer for me was:

rm .git/hooks/pre-commit

Upvotes: 14

ALOUI MH
ALOUI MH

Reputation: 97

If you look at your package.json you will get a better idea about the problem. Husky is npm dependency so basically you got the error because for some reason it's not available. So, option one run

npm i

Or if you already did that

rm -rf node_modules
npm i

Upvotes: 0

Yaroslav
Yaroslav

Reputation: 714

Nothing helps me but remove local repo and clone it again. This kinda error appears when I accidentically tried commit and push changes to master..

Upvotes: 0

Yuniac
Yuniac

Reputation: 583

For anyone who is having this problem with husky, for me what solved it is running:

husky install

(which I forgot to run) The docs talk about it under usage, aka npm run prepare for one time after you set up husky. usage

Upvotes: 0

Rohit Gupta
Rohit Gupta

Reputation: 4185

If the script files are actually present, then the cause may be that the first line of the script should be something like:

#!/bin/sh

Otherwise you get the same error.

Any file will work -- e.g., /bin/bash, /usr/local/bin/python3 -- as long as it exists and is executable.

Upvotes: 12

Serhii Harbovskyi
Serhii Harbovskyi

Reputation: 318

I got that error after I was played with brew to make yarn work in Webstorm.

The fix is just re-install pre-commit again.

brew install pre-commit

https://pre-commit.com/

Upvotes: 1

L. D. Nicolas May
L. D. Nicolas May

Reputation: 476

Working from macOS, my issue was that Python3 was originally installed via Homebrew, and after I upgraded Python3 via brew update && brew upgrade, I got the same error.

To fix this, I needed to update the symbolic links in the directory that the shebang (#!) line in the .git/hooks/pre-commit file points to.

Here's how I fixed this in my environment:

  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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python
    
    ln -s /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3.9
    
    ln -s /usr/local/Cellar/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    

Upvotes: 6

Amarnath Sagala
Amarnath Sagala

Reputation: 31

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

.git/hooks/pre-commit: line 9: py: command not found

.git/hooks/pre-commit: line 10: py: command not found

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

This command resolved my issue !!

Upvotes: -1

amir karimi
amir karimi

Reputation: 1

just make sure you have hooks folder inside of .git . if not make one.

Upvotes: -3

user7370241
user7370241

Reputation: 13

Deleting pre-commit from hooks inside .git worked for me

Upvotes: -3

gersonlimadev
gersonlimadev

Reputation: 319

You should delete node_modules folder and install again running npm install or yarn install. After it, you probably solve the problem.

Upvotes: 19

Mutant
Mutant

Reputation: 3819

pre-commit hook runs first when you try to commit changes, it can be used to do certain checks, tests, conditions. In this case, clearly, you don't have it, that's why it's complaining.

Go to .git/hooks directory and remove pre-commit file, as its trying to reference to node_modules/pre-commit/hook. It should resolve it.

Other option is append your commit with option: --no-verify, it will make the commit without checking.

Upvotes: 134

Related Questions