How to never commit a temporary code to your codebase.

Andrey Kazarovets
Sep 10 · 4 min read
val isUserPremium = true //userRepo.isPremium() NOCOMMIT
~/demoapp: git commit -a -m 'testapp/src/main/java/com/demoapp/DemoClass.kt:1: 
val isUserPremium = true //userRepo.isPremium() NOCOMMIT
ERROR: @COMMITFAIL or @NOCOMMIT found. Exiting to save you from yourself.


Now let’s see how can we add this behavior to our projects.

#!/bin/bash FILES_PATTERN='(\..+)?$'
if ( git diff --cached --name-only | grep -E $FILES_PATTERN | xargs grep -E --with-filename -n $FORBIDDEN ); then
echo "ERROR: @COMMITFAIL or @NOCOMMIT found. Exiting to save you from yourself."
exit 1
chmod +x pre-commit

Adding the hook to every local repository

I work with this hook on every repository I have. And I don’t want to add it manually every time I clone a new repo, so why not to delegate this work to git? If you use Git v2.9 and above you can write following in console (assuming you have your hooks in githooks folder):

git config core.hooksPath githooks

Live template to make it safer

We can’t be sure we won’t make a mistake writing the keyword, so it’s a good idea to create a live template that will help you with it.

  1. Choose any group you like and press + -> Live Template to create a new template
  2. Select abbreviation (I chose ncm), any description you want and put your keyword in Template text (I put NOCOMMIT)
  3. Define applicable contexts (the place where you template will work). Here we should choose Java -> Comment, Kotlin -> Comment
  4. Make sure it’s checked in the menu above.
  5. Apply -> OK

Extend it as you want

You can also expand the idea as you wish, the power beyond git hooks is really enormous.

    Andrey Kazarovets

    Written by

    Android Developer from Minsk/Belarus

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade