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.

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.

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.

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

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

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.

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.




















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!