Executing Git hooks on Windows

By default, Git for Windows executes hook scripts using its own Windows port of the bash shell. Certainly, a Unix shell has no idea about %1. Supposedly, Git for Windows has extra hacks in place to detect "common" filename extensions — such as .bat — and take an alternate route in such a case.

I think your fix to your own program is the best, but another approach would be to rewrite your script to read

#!/bin/sh
c:/Programs/PHP/php.exe c:/Data/Scripts/git-pre-push.phpcli "$@"

(the shebang line has no real special sense under Windows other than hinting the next person to edit the script about the meaning of its content).


On Windows, normally the special '#!' (called shebang) first line in PHP script has no effect . However, Git on Windows is able to recognize the shebang first line. You can write a commit-msg hook as below using PHP, it will be running on Windows properly.

#!D:/php/php.exe

<?php
echo 'commit-msg';
exit( 0 );    

See more on git hooks on windows


#!/bin/sh
echo "executing pre-commit"

# Instructions:

# Put this file into your .git/hooks folder and set as executable 

 #- for Windows (attrib +x pre-commit)
 #- for ubuntu (chmod +x pre-commit)

# If you want to skip the hook just add the --no-verify: git commit --no-verify

# ---------------------------------------------

# Modify this
# LIST='list\|of\|words\|splitted\|by\|slash\|and\|pipe'
LIST="puts\|debugger;\|binding.pry\|alert(\|console.log("

if git rev-parse --verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

for FILE in `git diff-index --name-status --cached $against -- | cut -c3-` ; do
    # Check if the file contains one of the words in LIST
    if grep -w $LIST $FILE; then
      echo $FILE." has unwanted word. Please remove it. If you want to skip that then run git commit -m '"your comment"' --no-verify"
      exit 1
    fi
      done
exit