ConfigGenerator

How to Create Git Hooks

Learn how to automate local repository checks using native Git hooks or tools like Husky.

What are Git Hooks?

Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They are used to enforce code quality locally.

\n

Creating Native Hooks

Navigate to the hidden .git/hooks directory in your repo. You will find sample files ending in .sample. Remove the .sample extension and edit the script to execute your desired commands (e.g., running tests before a push).

\n

Making Hooks Executable

Native hooks must be executable. Run chmod +x .git/hooks/pre-commit on Unix-based systems to ensure Git can execute the script.

\n

Sharing Hooks with Husky

Because the .git directory is not committed, native hooks cannot be easily shared. In JavaScript projects, developers use Husky to configure hooks in package.json, which automatically installs them for the whole team.