Git pre-commit hook doesn't work in Windows

Viewed 786

Pre-commit hooks just doesn't work in Windows. I tried all the possible ways found in this link: Git hook not running on windows

I have my pre-commit file inside .git/hooks/pre-commit:

#!/bin/bash

echo "Hello, World!"

Steps I followed to test:

  1. Updated a file
  2. git add .
  3. git commit - m "test"

I tried both sh and bash but still no luck. What could be wrong? How to fix this?

2 Answers

I just tested your pre-commit hook in a simple CMD, with git version 2.30.1.windows.1 (not even the latest Git For Windows)

It just works.
I even added an exit 1 (to not commit the test commit I was making)

But it does work (even on Windows, calling bash) if bash.exe is in your %PATH%

where bash
C:\path\to\git\bin\bash.exe

I am using the bash included in Git For Windows, even though recent Windows 10 do have their own bash.exe.

I had a similar problem with husky pre-commit hook not working in GitHub Desktop client on Windows.

The error was:

/usr/bin/env: 'bash': No such file of directory husky - pre-commit hook exited with code 127 (error)

Was fixed by adding C:\Program Files\Git\bin to PATH for current user and entire PC

Related