How to use a global git hook for all your projects

Alan
1 min readDec 11, 2020

I work on a few php projects and I use the famous var_dump for debugging.

Sometimes when we are in a rush we commit lines that have var_dump on accident.

How can we prevent this?

The solution is using git hooks.

Here’s a quick example of how to use them.

Navigate to your root directory and create a global git hooks directory.

mkdir .git_template
mkdir .git_template/hooks

Create a git pre-commit hook.

cd .git_template/hookstouch pre-commit

Create a bash snippet where you can define what text to search/validate before we actually commit a file. Here is what I am using:

#!/bin/bashVAR=$(git diff --cached | grep -w "var_dump")
if [ ! -z "$VAR" ]; then
echo "You've left a var_dump in one of your files! Aborting commit..."
exit 1
fi

Make the pre-commit hook an executable:

chmod a+x .git_template/hooks/pre-commit

The last step is to go into our project directory and run the git init command. Don’t worry, this will not delete anything.

Credits:

--

--