Skip to content

Instantly share code, notes, and snippets.

@martinos
Last active December 29, 2015 14:09
Show Gist options
  • Select an option

  • Save martinos/7681810 to your computer and use it in GitHub Desktop.

Select an option

Save martinos/7681810 to your computer and use it in GitHub Desktop.
Protect your git repo against debugging code

If you don't want to commit changes that contains debugger or binding.pry into the repo. Add the following code in ~/.git_template/hooks/pre-commit

#! /usr/bin/env bash

ruby_files=`git status --short | grep -v '^D' | awk '{print $2}' | grep '.rb$'`

while read -r file; do
  result=`grep -rin 'binding.pry\|debugger' $file`
  
  if [ -n "$result" ]
  then
    echo "Cannot be commited because: $result ."
    exit 1
  fi
done <<< "$ruby_files"

Then:

chmod u+x ~/.git_template/hooks/pre-commit

Your hook will be installed the next time that you git init. You can even use git init on an existing repo.

The next time that you will try to commit an unwanted word, you will warned and the commit will be aborted.

If you want to commit your changes anyway, you can:

git commit -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment