Effortless GitHub Pre Commit Hook Setup Guide

Logan Jul 13, 2026

Setting up pre-commit hooks on GitHub is a powerful way to automate tasks and maintain code quality. Pre-commit hooks are scripts that run automatically every time you commit changes. This mechanism helps you catch issues early, reinforcing good coding practices and saving time in the long run.

git commit Explained (Day 6 Git Guide)
git commit Explained (Day 6 Git Guide)

In this guide, we'll outline a step-by-step process to set up a pre-commit hook using husky, a popular choice for Git hooks, and lint-staged, a pipeline to run linters on git tracked files. This duo will ensure your commits are clean and your codebase maintains a consistent style.

Git & GitHub Cheat Sheet 🔥 | Commands, Workflow, Branching & Version Control (Beginner to Pro)
Git & GitHub Cheat Sheet 🔥 | Commands, Workflow, Branching & Version Control (Beginner to Pro)

Setting up husky and lint-staged

Both husky and lint-staged are npm packages, so we'll start by installing them globally via npm or yarn. It's as simple as running:

The Ultimate Git Commands Cheat Sheet for Developers 🚀💻 #git #command #code
The Ultimate Git Commands Cheat Sheet for Developers 🚀💻 #git #command #code

npm install husky lint-staged --save-dev or yarn add husky lint-staged --dev

Installing husky

GIT Commands: Your Essential Skills Guide 🚀
GIT Commands: Your Essential Skills Guide 🚀

Husky installs Git hooks directly into your npm scripts. First, install husky as a dev dependency, then enable Git hooks using the following command:

npx husky install

This command will install husky in your project root and enable Git hooks by default.

a black and white silhouette of a cat
a black and white silhouette of a cat

Configuring lint-staged

Next, configure lint-staged in your package.json. Open your package.json file and add the following configuration:

"lint-staged": { "**.{js,jsx}": [ "eslint", "git add" ] }}

the github chat sheet is displayed in pink and black
the github chat sheet is displayed in pink and black

This configuration tells lint-staged to run ESLint on all JavaScript and JSX files before they can be committed. The "git add" command is necessary to stage the files after linting.

Setting up pre-commit hooks

25 GIT COMMANDS EVERY DEVELOPER SHOULD MASTER
25 GIT COMMANDS EVERY DEVELOPER SHOULD MASTER
Git Workflow
Git Workflow
an image of a computer screen with many different things on it
an image of a computer screen with many different things on it
Github Profile 2
Github Profile 2
Git Hub Cheat Sheet
Git Hub Cheat Sheet
Understanding Git and GitHub
Understanding Git and GitHub
Why React Hooks, and how did we even get here?
Why React Hooks, and how did we even get here?
15 Git Commands Every Developer Must Know
15 Git Commands Every Developer Must Know
the differences between github and github in web design, which one should use?
the differences between github and github in web design, which one should use?
a diagram that shows the flow of people in a network, and how to use it
a diagram that shows the flow of people in a network, and how to use it
Master Git Fast: 2-Hour Step-by-Step Cheat Sheet
Master Git Fast: 2-Hour Step-by-Step Cheat Sheet
Git
Git
Home Server Rack, Hacker Aesthetic, Home Lab, Props Concept, Dj Setup, Server Room, Maker Project, Equipment Storage, Electrical Panel
Home Server Rack, Hacker Aesthetic, Home Lab, Props Concept, Dj Setup, Server Room, Maker Project, Equipment Storage, Electrical Panel
Top 15 Git Commands Cheatsheet
Top 15 Git Commands Cheatsheet
a wall mounted circuit board with wires and electronic equipment
a wall mounted circuit board with wires and electronic equipment
Quick Tip: Git Commands Every Dev Should Know
Quick Tip: Git Commands Every Dev Should Know
two computer monitors sitting next to each other on top of a desk in a room
two computer monitors sitting next to each other on top of a desk in a room
two computer monitors sitting on top of a desk next to a keyboard and monitor screen
two computer monitors sitting on top of a desk next to a keyboard and monitor screen
10 Viral YouTube Hook Formulas to Get More Views Fast
10 Viral YouTube Hook Formulas to Get More Views Fast
two computer monitors sitting on top of a desk in front of a keyboard and mouse
two computer monitors sitting on top of a desk in front of a keyboard and mouse

Now that we have the foundations in place, let's set up the pre-commit hook. Husky will handle the pre-commit hook based on our configured lint-staged script.

No additional configuration is needed for husky to run lint-staged as a pre-commit hook. When you commit changes, husky will automatically run the lint-staged script on staged files.

Testing the pre-commit hook

To test that everything is working as expected, introduce a linting error into one of your JavaScript files. Then, try staging and committing the file. If the pre-commit hook is set up correctly, you should see linting errors printed in the console, and the commit should be aborted.

Once you've fixed the linting error, you should be able to commit your changes without any issues.

And that's it! You've successfully set up pre-commit hooks to ensure your code adheres to best practices before every commit. Happy coding!