3

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?

kittu
  • 6,662
  • 21
  • 91
  • 185
  • 1
    Windows isn't Linux so calling `/bin/bash` would result in an error. Try that on Powershell and you would see the error. Windows it should be on the `C:\Windows\System32\bash.exe`. – Prav Apr 17 '21 at 11:24
  • First, are you using a shell or the command prompt? just to make sure, check that `pre-commit` has execution permissions with `chmod +x pre-commit`. – Mattia Righetti Apr 17 '21 at 14:18

2 Answers2

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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

Michael Klishevich
  • 1,774
  • 1
  • 17
  • 17