Branch Protection and Hooks
Level 5 · Working as a team
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 contentsLesson 12 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Set rules on the main branch
- Write a pre-commit hook
- Say why a hook is not enough
12 mincovers 3 glossary entries
Worth reading first: Pull Requests and Forks
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.
$ cat .git/hooks/pre-commit
#!/bin/sh
npm test || exit 1
$ chmod +x .git/hooks/pre-commitWorked 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.
The words for this lesson3
Check yourself
Why is a pre-commit hook not enough to protect main?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.