# Git Hooks

> Automate code quality checks locally before your code is ever committed.

Subject: Version Control | Lesson 29 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-hooks
Previous: [Bisecting Bugs](https://codewithanimation.in/git/git-bisect.md)

## The Local Gatekeepers

Git hooks are custom scripts that Git automatically executes before or after critical lifecycle events such as committing (`pre-commit`), pushing (`pre-push`), or receiving code.

They allow developers to intercept the Git workflow to run custom logic, validate changes, or trigger external actions automatically.

## Why it matters

For frontend developers, Git hooks are the ultimate automation tool for enforcing code quality.

By using hooks, you can automatically run linters (ESLint), format code (Prettier), and enforce commit structures *before* bad code makes it into the repository. It shifts quality control left, saving time in CI pipelines.

## How it works (Husky)

Native Git hooks live in the hidden `.git/hooks` directory, which isn't shared with the team when they clone the repository.

To solve this, web teams use **Husky**. Husky configures Git to use a version-controlled directory (like `.husky/`) for its hooks. When developers run `npm install`, Husky automatically initializes these hooks so everyone has the exact same setup.

## Common mistake: The Bypassed Gate

The #1 common mistake is treating local Git hooks as a bulletproof security gate.

Developers can easily bypass local hooks using the `--no-verify` flag (e.g., `git commit -m "wip" --no-verify`) when in a rush. If a team relies *only* on local hooks without enforcing those same checks in their CI/CD pipeline, broken code will inevitably be merged.

## Tip: The Spellchecker vs Editor-in-Chief

Mental Model: Think of Git hooks as the **Spellchecker** and your CI/CD pipeline as the **Editor-in-Chief**.

Local hooks are the spellchecker sitting on your shoulder, catching syntax errors fast. CI/CD is the strict Editor-in-Chiefâ€”the non-negotiable gateway that officially rejects broken code from entering the main branch.

## Try it: Inspect a commit hook

Compare a failing check, a corrected change and an explicit hook bypass in this simulation.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-hooks

## Check yourself

### Question 1

Which of the following best describes how Husky solves the main limitation of native Git hooks for teams?

- A. It rewrites the Git core binary to automatically ignore failing frontend tests.
- B. It uploads your local `.git/hooks` folder to a remote server on every commit.
- C. It points Git to a version-controlled hooks directory that installs automatically via npm.
- D. It runs your pre-commit hooks in the cloud rather than on your local machine.

Correct answer: C

Husky changes the Git configuration to point to a folder tracked in your repo, ensuring all developers get the identical hooks setup automatically.

- Why not A: Husky does not modify Git's core binaries.
- Why not B: The native `.git/hooks` folder is still ignored.
- Why not D: Git hooks run entirely locally.

### Question 2

What is the most common pitfall when a team relies solely on local Git hooks for enforcing code quality?

- A. Hooks only run after the code is already pushed, meaning bad code is already shared.
- B. Developers can easily bypass local hooks using the `--no-verify` flag, so hooks must always be backed by CI/CD checks.
- C. Hooks require scripts written in Python, which is rarely used in frontend projects.
- D. They only work on macOS and Linux, leaving Windows developers completely excluded.

Correct answer: B

Because developers control their local environment, they can always skip hooks. CI/CD is required for strict, guaranteed enforcement.

- Why not A: Pre-commit hooks run before code is even committed.
- Why not C: Hooks are typically shell scripts and can run Node commands easily.
- Why not D: Husky supports Windows as well.

### Question 3

If you want to automatically run Prettier and ESLint exactly when a developer types `git commit`, which hook should you configure?

- A. pre-push
- B. post-commit
- C. pre-commit
- D. commit-msg

Correct answer: C

The `pre-commit` hook is triggered before the commit object is created. It is the industry standard hook for running linters and formatters.

- Why not A: This runs before pushing to a remote.
- Why not B: This runs after the commit is already saved to history.
- Why not D: This is used for linting the commit message itself, not the code.

## Remember this

- Hooks are scripts that run on Git lifecycle events.
- Husky makes it easy to share hooks across a web team.
- Hooks provide fast local feedback (the spellchecker).
- Never rely solely on hooks for security; always use CI/CD.
