Have you ever submitted your code for review, left to go do some more work and later came back to see a several dozen comments about how you forget to indent your code in several files, you forgot to add spacing between some key-value pairs, you forgot to run your tests and forgot to update one test file? I have. You may have even experience this from the other side as code reviewer.
Don’t you wish you would have caught all those minor issues before you commited your code? Of course, we all do. We’re all human and we all have a tendency to forget things, so don’t feel about this it happens to every single engineer I’ve worked with.
Good news though. There is a solution to this problem
What are Git Precommit Hooks?
Git precommit hooks allow you to hook into git to run one or multiple scripts before committing your’re code with no action needed on your part - yay! On my team we run multiple scripts for our frontend code bases right before we commit our code and if any of the precommit script fails we prevent each other from committing. Here are some scripts we run for as part of our precommit hook:
- lint our Sass stylesheets with stylelint
Why You Should Use Git Commit Hooks?
- it saves you & your team time by allowing you to focus on the important aspects of your code.
- cleaner code reviews since the feedback you’ll be receiving isn’t about small knitpickky things like codestyle.
- it decreases the likelihood of bad code being introduced into your code base.