August 17, 2023

Git Hooks: pre-commit Hook for ESLint using Python

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.

What are Git Hooks?

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.

Installing Hooks

Hooks are located in the ‘.git/hooksdirectory 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.

Pre-commit Hook

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.

1. Setup a Project :

Lets create a sample nodeJS project using simple steps.

  • Create a new directory for the project and navigate to that folder in command prompt or terminal.
  • Execute ‘npm init’ and fill in the options or press enter/return to choose default values.
Create a project using npm

Initialize git repository using ‘git init’ in Git Bash.

Initialize Git Repository

  • Create an app.js file in root directory and add some sample code.

2. Install and Initialize the ESLint :

  • You can install ESLint using npm by executing the command: npm install -g eslint
  • Initialize ESLint.

  • For a detailed explanation of coding standards and ESLint, you can visit this article.
  • I’ve added some rules to .eslintrc.json file but this is optional while using style guide like Airbnb.

3. Install Pre-commit Hook

  • You’ll find pre-commit.sample file in ‘.git/hooks’ directory of your project.
  • In case you don’t see .git directory in your project folder, you might have to unhide it. If you are using VS Code as your text editor, you can unhide directory as follows :
    File -> Preferences -> Settings -> Text Editor -> Files -> Delete ‘**/.git’ from Exclude section.

  • Let’s install the hook by removing ‘.sample’ extension in ‘.git/hooks’.
  • You’ll see a default shell script on opening the pre-commit file. Let’s write some Python code to execute ESLint on files that are staged to commit.

pre-commit file :

4. Commit Changes :

  • Let’s now try to add and commit the changes we’ve made so far.
  • The commit is aborted because we have some ESLint errors in our javascript files.
  • Let’s fix them and try to commit again.

  • Now the commit is successful, which means we’ve passed the ESLint check.

Scope of Git Hooks

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.

August 17, 2023
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles