Execute git pre-commit hook only if files in certain directory modified

Solution 1:

try,

git diff --cached --name-only | if grep --quiet "$SRC_PATTERN"
then
    ...

Happy hacking

Solution 2:

Try it like this:

SRC_PATTERN="site/assets/js/"
if git diff --cached --name-only | grep --quiet "$SRC_PATTERN"
then
  echo "none"
  exit 0
fi

This looks a lot like the accepted answer, but that one is wrong. In bash/sh each command in a pipeline is executed in a separate subshell, so an exit in the then part will only exit the subshell, any code that follows the fi will still be executed!