Avoiding blacklist keywords in commits — Git
Adding hooks before committing your code to avoid debugging code in Git.
I have been in a couple of situation wherein I had committed ‘byebug’ or ‘debugger’ in commit by mistake. So, I needed a solution in which I can blacklist keywords while committing.
GitHub provides hooks, that you can use to trigger certain events. Below steps will take you through the setup process to add a pre-commit hook to avoid blacklist words from committing.
1. Save the below file as pre-commit
https://gist.github.com/maanavshah/d0e5960c7c4ec48b757f92104b9f6c7e
2. Put the pre-commit file (without any extension) in the .git folder of your GitHub repository
$ mv pre-commit ~/myapps/app/.git/hooks/
3. Change file mode to allow execution by git
$ chmod +x ~/myapps/app/.git/hooks/pre-commit
4. Usage
$ git commit -m "Add debugger command"
It will not allow adding commit if your any of file changes have strings from the blacklist defined.
However, there is always a hack. You can skip hook by below command.
$ git commit --no-verify -m "I understand risk"
Further:
You can add a hook for all your GitHub repository like below –
- Enabled Git templates
$ git config -global init.templatedir '~/.git-templates'
2. Create a directory for the global hooks
$ mkdir -p ~/.git-templates/hooks
3. Copy the pre-commit file at this location
$ cp ~/myapps/myapp/.git/hooks/pre-commit ~/.git-templates/hooks/
That’s it. Hope this helps.
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you!