Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my git pre-commit hook running?

Tags:

git

husky

Up until recently -- I only noticed this a couple days ago -- my git pre-commit hook was working. I'm writing a react app and using Husky, TSLint, and Prettier to clean and lint my code before committing. Now, when I change and commit files, the pre-commit hook doesn't run.

My project structure looks like this:

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

If I manually execute the hook, it seems to run fine:

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

But when I actually try to commit, husky doesn't run:

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

[/project/react] # 

Any idea why it isn't running?

like image 495
whiterook6 Avatar asked Jul 17 '19 22:07

whiterook6


2 Answers

Updating Husky by running yarn add --dev husky fixed the problem. I have no idea why it stopped working, but husky was very out of date anyways.

like image 198
whiterook6 Avatar answered Nov 15 '22 01:11

whiterook6


Check if git config core.hooksPath has been set to a different path than its default: $GIT_DIR/hooks

Check also that GIT_DIR (environment variable) is not currently set.

In both cases, Git would look for that hook not where you would expect (and currently have your pre-commit hook)

like image 32
VonC Avatar answered Nov 15 '22 00:11

VonC