Using global pre-commit hook to prevent committing unwanted code

Benoît Ripoche
3 min readSep 20, 2018

--

Most of the time, coding is all about reading a lot of documentation, doing research on forums or specialized sites, then doing dozens of tests before finally writing the few effective final lines of code!

For my part, during this test phase, I write a lot of useless code, comments, debugging, old functional code… It’s dirty, it’s badly written, badly indented, but once the functionality is finished, I try to make my code as clean and readable as possible.

The problem is that sometimes, when working on fairly large projects, with dozens of pages open at the same time on which we work, it can happen to forget to remove unwanted code that will be commited and written forever in the history of the project code!

It doesn’t have to be dramatic. A “console.log(‘test’)” or “dump($var);” has never killed anyone… unless these debug messages reveal security sensitive information, or when the trainee who has struggled to develop a feature, has forgotten a “console.log(‘its working mother f**kers!’)” in the middle of the code, and the client falls on it.

When the client discovers some dirty code💩 Commits never forgets

To prevent this, there are different solutions. The first is obviously to be as rigorous as possible and to never write things that you would’nt want to see commited.

For the less rigorous, of which I am a part, it’s possible to set up a system with a keyword that prevents you from committing your code.

We will explore this method with the implementation of a global pre-commit hook.

Setting up

Prerequisites: Git >= 2.9

1: Create a folder where to store global hooks (ex ‘/home/user/gitconfig/hooks’)

2: Create a ‘pre-commit’ file in folder you just created and paste the following code:

#
# To prevent debug code from being accidentally committed, simply add a comment near your
# debug code containing the keyword !nocommit and this script will abort the commit.
#
if git commit -v --dry-run | grep '!nocommit' >/dev/null 2>&1
then
echo "Trying to commit non-committable code."
echo "Remove the !nocommit string and try again."
exit 1
else
exit 0
fi

Note: This code is from Arturadib => https://gist.github.com/arturadib/97a17752301ee796f9a0

Note: If you use locals pre-commit hooks, you have to set your global pre-commit hook like this in order to make the local ones working:

#
# To prevent debug code from being accidentally committed, simply add a comment near your
# debug code containing the keyword !nocommit and this script will abort the commit.
#
if git commit -v --dry-run | grep '!nocommit' >/dev/null 2>&1
then
echo "Trying to commit non-committable code."
echo "Remove the !nocommit string and try again."
exit 1
else
# Run local pre-commit hook if exists
if [ -e ./.git/hooks/pre-commit ]; then
./.git/hooks/pre-commit "$@"
else
exit 0
fi
fi

3: Edit your global Git config file to add the following lines:

[core]
hooksPath = /home/user/gitconfig/hooks

Note: Change the value of ‘hooksPath’ depending on the folder you created on step 1.

Note: The location of your global Git config file (‘.gitconfig’) depends on your operating system. If you have trouble finding it, ask your favorite search engine :)

Usage

When writing code that you don’t want to commit later, add a comment with the “!nocommit” keyword.

Exemple of using the “!nocommit” keyword.

Note: In your IDE, you can try to highlight this specific keyword to make it easier to locate as in the picture above.
For example, in PHP Storm, you can create a text pattern in Settings > editor > TODO

So, if we forget to delete the unwanted code, the pre-commit hook will prevent the commit!

Pre-commit hook in action!

--

--