Get more updates and further details about your project right in your mailbox.
The best time to establish protocols with your clients is when you onboard them.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.
Git, one of the most widely used Version Control System(VCS), allows multiple developers to coordinate and contribute to a single project. It allows developers to customize git actions to meet their own organizational coding standards or quality requirements. Git Hooks are one of the customizations offered by git. They can also be used to build a CI/CD pipeline for your projects.
Git hooks are just a bunch of scripts that run automatically when a certain event occurs in your repository. There are two main types of hooks, namely, Client-side Hooks and Server-side Hooks. Simply put, Client-side hooks are scripts that run on your local machine whereas the Server-side hooks are scripts that execute on git server.
Hooks are located in the ‘.git/hooks’ directory of a git repository. By default, git populates this directory with a bunch of .sample files, which are actually shell scripts.
To install a certain hook, remove its ‘.sample’ extension. There are default scripts provided for each script by git but you can write your own scripts in your preferred scripting language as per your standards.
In this article, we’ll focus only on pre-commit hook. Git’s pre-commit hook executes every time you run ‘git commit’ before git generates a commit object. This hook can be used for static analysis of code being committed, linting, spell-checks, code-styles etc. It is used to make sure you don’t unexpectedly push unformatted code or violate organizational coding standards.
In this article, we’ll implement git’s pre-commit hook for ESLint’ing our code to demonstrate the use of git hooks.
ESLint is a static code analysis tool for identifying problematic patterns found in JavaScript code, created by ‘Nicholas C. Zakas’. Rules in ESLint are configurable, and customized rules can be defined and loaded. ESLint covers both code quality and coding style issues.
Lets create a sample nodeJS project using simple steps.
Initialize git repository using ‘git init’ in Git Bash.
pre-commit file :
The ‘.git/hooks’ directory cannot be cloned along with other project files by using ‘git clone’. Hence, how to share these hooks among a team of remote developers is question that exists. There are certain ways to configure them. A simple solution is to store them inside another project directory(above the ‘.git’ directory) and push them to git. Any other remote developer of same project can install a hook simply by copying from this new directory and pasting them into respective hooks of ‘.git/hooks’ directory. This way, all the developers can follow these scripts and the scripts themselves will be version-controlled.
Using Git Hooks is a simple and easy way to alter git’s internal behavior such that it meets the expectations of your project. These hooks can help act as checkpoints to maintain code quality. Writing effective hooks is not easy, but mastering it will save you loads of manual work.