What are Git Hooks?
Git hooks are shell scripts that run automatically before or after git executes an important
command like Commit or Push. You can find hooks in your
.git folder. When you
$ git init,
that command generates a number of hooks which define how git is able to write from your local to
your remote repository.
A description of some of the hooks above are highlighted in the table below . Depending on your software requirements and your knowledge of Bash, you can harness the full potential of these hooks and also create custom hooks. For more info, read this article on creating custom pre-commit hooks Implement your own Pre-commit Hooks.
|Git Hook||Git command||Usage|
|post-update.sample||git push||By updating all data after the push|
|commit-msg.sample||git commit||To set the message of a commit action|
|pre-commit.sample||git commit||Before committing|
|prepare-commit-msg.sample||git commit||When a commit message is set|
|pre-push.sample||git push||Before making a push|
|pre-receive.sample||git push||When we push and get the data from the remote repository|
|update.sample||git push||By updating the remote data in a push|
Pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. In this wiki, we will highlight a few packages which are useful for static code analysis. Some of which are the very hooks used in the pre-commit hooks.
.pre-commit-config.yaml file within your project and use in
multiple projects. This file contains the pre-commit hooks you want to run every time before you
commit. It looks like this 
Why use Pre-commit hooks?
- Improve the quality of commits, obviously.
- Very useful in production environments for code styling and compliance.
- These hooks help automate static code analysis thereby informing the developer of potential issues within the code.
Type of hooks
Some hooks are specific to programming languages other than Python and R. However, most of these hooks can be used in generic projects to check other non-specific programming scripts.
- R: lintr
- Python: pylint
Code quality and styling (automated)
- R: styler, tidyverse_style
- Python: black, flake8
Installation in R
For full set up guide see in link Install and use pre-commit hooks in R.
For full set up guide see in link Install and use pre-commit hooks in python.
Pre-commits in action
The typical results when you commit a repo with pre-commit hooks are Passed, Failed, Skipped.
"Skipped" denotes there is nothing to check perhaps because the file is missing or you have
$ git add the necessary files.
Snapshots of pre-commit hooks in use (R and Python).
Leave a Reply
Your email address will not be published. Required fields are marked *