The Ultimate Git Pre Commit Hook Tutorial For Beginners

Logan Jul 13, 2026

Enhancing your Git workflow can lead to improved code quality and collaboration among your team. One powerful feature Git offers is pre-commit hooks, which allow you to define local rules for your project. These hooks can perform actions like running linters, checking code formatting, or failing the commit altogether if certain conditions aren't met. In this tutorial, we'll guide you through setting up a pre-commit hook for running a linter in your project.

Nitin Gupta (@niting786) / X
Nitin Gupta (@niting786) / X

Before we dive in, make sure you have Git installed on your machine. If not, you can download it from the official website: https://git-scm.com/downloads. Additionally, for this tutorial, we'll use ESLint as our linter, which is widely used for catching computer programming errors in JavaScript.

Git & GitHub Commands Cheat Sheet ๐Ÿ’ป | Essential Developer Guide
Git & GitHub Commands Cheat Sheet ๐Ÿ’ป | Essential Developer Guide

Understanding Git Hooks

Git hooks are custom scripts that Git executes at various stages of the Git life cycle. Pre-commit hooks run just before you apply a change, giving you the chance to validate the changes you're about to commit. This helps ensure that your project maintains a consistent coding standard and catches potential issues early.

Best Viral Hooks
Best Viral Hooks

To explore where Git stores these hooks, you can navigate to the `.git/hooks` directory in your project root. If you don't see a `pre-commit` file, that's because Git creates hook files with executable permissions when you create or enable them. Now let's proceed to set up our pre-commit hook.

Setting Up the Pre-commit Hook

a computer screen with the text stop forgeting git
a computer screen with the text stop forgeting git

First, we'll create our pre-commit hook script. Make sure you're in your project root directory (where your `.git` folder resides) and create a file called `pre-commit`. You can use your preferred text editor, but ensure the file is writeable by the user.

Now, let's make this file executable by running the following command in your terminal:

chmod +x .git/hooks/pre-commit

Configuring the Linter with ESLint

a woman standing in front of a door brushing her teeth with the words visual hook to try
a woman standing in front of a door brushing her teeth with the words visual hook to try

With the pre-commit hook file created, we'll configure it to run ESLint. Start by installing ESLint and the necessary dependencies as development devDependencies in your project:

npm install --save-dev eslint

Then, initialize ESLint in your project by running:

npx eslint --init

Follow the prompts to create your `.eslintrc` configuration file. For this tutorial, we'll choose the JavaScript standard style guide. Once finished, you'll have a working ESLint configuration.

How to Write Attention-Grabbing Hooks Like a Pro
How to Write Attention-Grabbing Hooks Like a Pro

Now, let's edit our `pre-commit` file to run ESLint. Open the `pre-commit` file in a text editor and add the following script:

#!/bin/sh

# Redirect output to stderr.
exec eslint "$@" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "ESLint found linting errors! Fix them and try again."
    exit 1
fi
exit 0

This script runs ESLint on the staged changes (represented by "$@") and checks if it exits with a non-zero status (i.e., if it finds errors). If errors are found, it prints a message to stderr and exits the script with a non-zero status, preventing the commit.

Good Hooks vs Bad Hooks: Viral Content Writing Tips That Boost Engagement
Good Hooks vs Bad Hooks: Viral Content Writing Tips That Boost Engagement
Tips for Tying a Strong Tarpaulin
Tips for Tying a Strong Tarpaulin
The missing link to your videosโ€ฆ A VISUAL HOOK ๐Ÿช A visual hook is a pattern interrupt that makes your audience pause and watch. Whether itโ€™s an object or a bold title card, it must connect directly to your videoโ€™s message. The goal? Grab attention in the first 3 seconds. ๐Ÿ•’ Build curiosity, boost your watch time, and get your videos pushed to more viewers. ๐Ÿš€ Want to increase your engagement? Start using visual hooks that captivate! ๐Ÿ’ก ๐Ÿ‘‡ Follow us for more video strategies that convert! Engaging Hook, Hooks For Videos, Creative Social Media Hooks, Visual Hooks For Instagram Reels, Hooks Video, Hook Video Ideas, Video Hooks, Video Hooks For Reels, Visual Hook Video
The missing link to your videosโ€ฆ A VISUAL HOOK ๐Ÿช A visual hook is a pattern interrupt that makes your audience pause and watch. Whether itโ€™s an object or a bold title card, it must connect directly to your videoโ€™s message. The goal? Grab attention in the first 3 seconds. ๐Ÿ•’ Build curiosity, boost your watch time, and get your videos pushed to more viewers. ๐Ÿš€ Want to increase your engagement? Start using visual hooks that captivate! ๐Ÿ’ก ๐Ÿ‘‡ Follow us for more video strategies that convert! Engaging Hook, Hooks For Videos, Creative Social Media Hooks, Visual Hooks For Instagram Reels, Hooks Video, Hook Video Ideas, Video Hooks, Video Hooks For Reels, Visual Hook Video
10 Inspiring Hooks for Your Next Tiktok or Reel
10 Inspiring Hooks for Your Next Tiktok or Reel
Visual Hooks (you should def use)๐ŸŽฅ
Visual Hooks (you should def use)๐ŸŽฅ
the words steal these 5 visual hooks are lit up against a black background with green squares
the words steal these 5 visual hooks are lit up against a black background with green squares
a cell phone with the text hook 3 on it and an image of a woman wearing a white t - shirt
a cell phone with the text hook 3 on it and an image of a woman wearing a white t - shirt
3 Proven Hooks to Start a Faceless YouTube Video (Hook = Views!)
3 Proven Hooks to Start a Faceless YouTube Video (Hook = Views!)
a person holding up a piece of paper
a person holding up a piece of paper
How to write an unforgettable hook
How to write an unforgettable hook
dogs hook
dogs hook
Viral Hooks to Explosion Your Views This Week ๐Ÿš€
Viral Hooks to Explosion Your Views This Week ๐Ÿš€
18 Hook Ideas for Content
18 Hook Ideas for Content
7 TikTok Hook Ideas That Instantly Grab Attention
7 TikTok Hook Ideas That Instantly Grab Attention
25 Video Hook Ideas That Work for Any Content or Niche
25 Video Hook Ideas That Work for Any Content or Niche
the words work on imppoing your hook
the words work on imppoing your hook
an image of a computer screen with some text on it that reads, support reactt, use state from reactt function counter
an image of a computer screen with some text on it that reads, support reactt, use state from reactt function counter
instructions for how to tie a kayak
instructions for how to tie a kayak
a woman standing in front of a white bookcase with her back to the camera
a woman standing in front of a white bookcase with her back to the camera
the text is written in black and white on a sheet of paper that says, 10 hooks that instantly grab attention
the text is written in black and white on a sheet of paper that says, 10 hooks that instantly grab attention

Enabling the Pre-commit Hook

With the configuration complete, let's test our pre-commit hook. First, however, we need to enable it. Git stores hooks as bare repositories, so we need to create a symbolic link to our pre-commit file:

Run the following command in your terminal:

ln -sf ../../hooks/pre-commit .git/hooks/pre-commit

Now, whenever you stage changes and attempt to commit, Git will run your pre-commit hook, executing ESLint on the staged files. If your code doesn't pass the linting rules, Git won't let you proceed with the commit.

Bypassing the Hook Temporarily

Sometimes, you might want to bypass the pre-commit hook temporarily, for instance, when working on a new feature or when you knowingly introduce temporary violations to meet a deadline. You can do this by appending `--no-verify` to your commit command:

git commit -m "Your commit message" --no-verify

This instructs Git to skip the pre-commit hook for that specific commit.

And there you have it โ€“ a powerful and flexible way to maintain code quality and ensure consistent coding standards throughout your project. Using pre-commit hooks not only helps catch issues early but also fosters a culture of code quality among your team. Explore and customize your hook to fit your project's needs, and happy coding!