Keeping unwanted code out of your Git repository
by Tad Lamb Jr
Sometimes you can include code for debugging that you do not want to have pushed to the production environment. Git allows for hooks to run against code as you are committing it to your repository.
In the .git/hooks
directory you will find several sample files that you can modify to run against your code.
Here is an example of the pre-commit
hook code that I use for a Rails project. I removed the .sample
extenstion, and I set the LIST variable to contain the list of strings that I don’t want to appear in my production code.
#!/bin/sh
LIST="puts\|debugger\|alert(\|console.log(\|byebug"
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
for FILE in `git diff-index --name-status --cached $against -- | cut -c3-` ; do
# Check if the file contains one of the words in LIST
if grep -w $LIST $FILE; then
echo $FILE " has one of the words you don't want to commit. Please remove it."
exit 1
fi
done
exit
But, what if I want to override this and include, say, a console.log()
in my production code? In that case, you can include --no-verify
in your commit command, and it wil skip the hook for that one commit.
Subscribe via RSS