This pre-commit hook is designed to be used for Drupal 7 and 8 sites.
Download the pre-commit file. Save it as .git/hook/pre-commit in your git repo. You need to ensure that the file is executable.
If you want this to be added to all new projects automatically, add it to your git init templates.
To install and PHP CodeSniffer for Drupal, please read the official documentation.
To see this working checking out this short YouTube video.