First, the hook must be in the .git/hook folder and the pre-commit name (with execute chmod +x .git/hooks/pre-commit : chmod +x .git/hooks/pre-commit )
OP jesusjjf confirms in the comments that there was a problem:
I copied what was in the pre-commit.sample file into a simple text file, and not just renamed it.
Secondly, here are some sample scripts:
You have another example using a similar technique, using git rev-parse and git diff-index and the empty tree that I mentioned earlier :
#!/bin/sh 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 $against -- | cut -c3-` ; do
Comments on this topic mention:
On my system, if [ "grep 'debugger' $FILE" ] always evaluated as true .
Changing this parameter to if grep -q 'debugger' "$FILE" fixes this.
A more recent example :
#!/bin/bash # Pre commit hook that prevents FORBIDDEN code from being commited. # Add unwanted code to the FORBIDDEN array as necessary FILES_PATTERN='\.(rb|js|coffee)(\..+)?$' FORBIDDEN=( debugger ruby-debug ) for i in "${FORBIDDEN[@]}" do git diff --cached --name-only| grep ".js" |xargs sed 's/ //g'|grep "ha_mobile.debug=true" && \ echo 'COMMIT REJECTED Found ha_mobile.debug=true references. Please remove them before commiting' && exit 1 git diff --cached --name-only | \ grep -E $FILES_PATTERN | \ GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $i && \ echo 'COMMIT REJECTED Found' $i 'references. Please remove them before commiting' && exit 1 done exit 0
source share