# Branch Protection and Hooks

> How branch protection keeps broken code off main, and how a Git hook runs a check before each commit. Why the hook alone cannot protect a team.

**Course:** [Git and GitHub](https://learn.benibix.com/learn/development-version-control)  
**Time:** 12 min

**After this lesson you can:**

- Set rules on the main branch
- Write a pre-commit hook
- Say why a hook is not enough

## How to keep broken code off main

Branch protection is a set of rules a Git host puts on one branch, usually main. It can ask for a pull request for every change. It can ask for at least 1 approving review, and for tests to pass before anything merges. On GitHub, each rule also blocks force pushes and deleting the branch, unless you turn that off.

The tests come from continuous integration, or CI. Each time someone pushes, a server builds the code and runs the tests on it. The pull request page shows a green tick or a red cross. With protection on, a red cross keeps the merge button locked.

Admins can skip the rules unless you say otherwise. GitHub has a setting to stop that, and it is worth turning on. The rushed Friday fix that skips review is the one most likely to break main.

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 the script ends with an error, Git stops and makes no commit. Other hooks run when a message is written, or before a push.

Hooks live in the .git/hooks folder of each copy, so they run on your own computer, before anything leaves it. That makes them fast. A hook can run the tests, check the layout of the code or look for a password, and stop you within seconds.

The catch is that hooks are not shared. Cloning a repo does not copy them, so each person has to set them up. And git commit --no-verify skips the pre-commit hook entirely. A hook is a helpful reminder for one person, not a rule for a team.

Many teams keep their hook scripts inside the repo, in a folder like .githooks. Each person then runs git config core.hooksPath .githooks once. It still depends on everyone doing that one step, which is why the check on the server stays.

So teams use both. The hook catches slips early, on your machine, in seconds. Branch protection with CI is the rule nobody can skip, on the server, before main. The first saves time, and the second keeps main safe.

```console
$ cat .git/hooks/pre-commit
#!/bin/sh
npm test || exit 1
$ chmod +x .git/hooks/pre-commit
```
A pre-commit hook that runs the tests. If they fail, the script ends with an error and Git makes no commit.

![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)

### Example: A team locks its main branch

Lucy's team of 5 turns on branch protection for main, with 1 approval and passing tests.

The next week, a teammate opens a pull request that breaks the login page. CI shows a red cross, the merge button stays grey, and the bug never reaches main.

**Words in this lesson:** [Branch Protection](https://learn.benibix.com/term/development/branch-protection), [Git Hook](https://learn.benibix.com/term/development/git-hook), [Continuous Integration (CI)](https://learn.benibix.com/term/development/continuous-integration)

> **The one thing to remember.** A hook runs on your own computer, and anyone can skip it. Branch protection runs on the server, and nobody can.

**Question:** Why is a pre-commit hook not enough to protect main?

**Answer:** Hooks are not shared by a clone, and --no-verify skips them. A hook lives in each person's own copy. A teammate may not have it, and anyone can skip it. Branch protection on the server is the check nobody can get around.

---

Source: https://learn.benibix.com/learn/development-version-control/branch-protection-and-hooks
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-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
