It's strong recommended to use pre-commit framework to help maintain a good code.
You can see more details directly in the site of the framework but a simple way to install is using homebrew.
brew install pre-commit
The specilized hooks used by terraform needs some packages too. Again the easiest way is using homebrew.
brew install pre-commit terraform-docs
If everything works correctly, before commiting, pre-commit will check all staged files, as in the example below.
$ git commit -am ""
Check for merge conflicts................................................Passed
Trim Trailing Whitespace.................................................Passed
Check JSON...........................................(no files to check)Skipped
Detect AWS Credentials...................................................Passed
Terraform fmt........................................(no files to check)Skipped
Terraform docs.......................................(no files to check)Skipped