There is a module, called pre-commit, which is working well, and it's easy to set up.
If you want to write valid code, make sure you use a linter. It's good practice to have some 'on save' linter to tell you what errors you made in the real time. But, besides that, it would be nice to have some tool to lint files when you try to commit them. Git has so-called hooks (actions), which you can trigger at certain points (before you commit, after the commit, before the push, etc.).
I created a demo, to show you how it works.