# Git Hook

> A script Git runs on its own, like a check just before each commit.

**Field:** Software Development  
**Category:** Version Control  
**Also known as:** Git Hooks, Pre-Commit Hook, pre-commit, commit-msg Hook

## Think of it like

Think of it like a guard at a door. Before you go through, the guard checks your ticket.

## Definition

A Git hook is a script Git runs by itself at a set moment. The pre-commit hook runs just before a commit is made. If it ends with an error, Git stops and the commit is not made. Other hooks run when a message is written or before a push. Hooks live in the .git/hooks folder, so each person's copy has its own.

## Example

Emma's pre-commit hook runs the tests. When a test fails, her commit is stopped until she fixes it.

## Example code

```bash
#!/bin/sh
# .git/hooks/pre-commit
npm test || exit 1
```

If the tests fail, the hook exits with 1 and the commit is stopped.

## Why it matters

It catches mistakes on your own machine, before anyone else sees them.

## Common mistake

Thinking a hook protects the whole team. Hooks are not shared by a clone, and git commit --no-verify skips them.

## Three principles

- **Runs on its own.** Git calls it at a set moment.
- **Can say no.** An error stops the commit.
- **Local only.** Each copy has its own hooks.

## Related terms

- [Commit](https://learn.benibix.com/term/development/commit)
- [Linter](https://learn.benibix.com/term/development/linter)
- [Continuous Integration (CI)](https://learn.benibix.com/term/development/continuous-integration)
- [Unit Test](https://learn.benibix.com/term/development/unit-test)

## Told apart from

- [Git Hook vs Branch Protection](https://learn.benibix.com/compare/git-hook-vs-branch-protection): People think a pre-commit hook keeps bad code off main, and it does not. A Git hook runs on one person's computer. A clone does not copy it, and git commit --no-verify skips it. Branch protection runs on the Git host. It locks main until reviews and tests pass, and nobody can skip it unless they are allowed to.

## Taught in

- [Branch Protection and Hooks](https://learn.benibix.com/learn/development-version-control/branch-protection-and-hooks): Git and GitHub, 12/13

![The command git commit leads into a box called the pre-commit hook, which runs the tests. Two paths come out. If the tests pass, the commit is made. If they fail, the commit is stopped and nothing is saved. A note says hooks live in the repository's .git/hooks folder.](https://learn.benibix.com/diagrams/dev3-git-hook.svg)

---

Source: https://learn.benibix.com/term/development/git-hook
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
