Git & GitHub
/Advanced
Branch Protection Rules
Definition
Rules enforced by GitHub that prevent certain actions on specific branches (usually `main` or `production`), such as preventing force pushes, preventing deletions, or requiring code reviews before merging.
Explain Like I'm New
A force field around your `main` branch. It physically stops developers from typing `git push origin main`. It forces everyone to use Pull Requests, forces them to get at least 1 approval from a coworker, and forces automated tests to pass before the code is allowed in.
Real World Example
A developer gets angry and tries to run `git push --force` to overwrite the company's production code. GitHub's branch protection intercepts the command, denies it, and logs the attempt.
Common Use Cases
- •Enforcing code quality
- •Compliance and security
- •Preventing accidents
Terminal Output
bash / terminal
/*
Common Branch Protection Settings you should enable on 'main':
[x] Require a pull request before merging
[x] Require approvals: 1
[x] Dismiss stale pull request approvals when new commits are pushed
[x] Require status checks to pass before merging
(This means your automated CI/CD Tests must pass!)
[x] Do not allow bypassing the above settings
[x] Restrict who can push to matching branches
*/
Interview Questions
basic
- Which branch is most commonly protected by these rules?
intermediate
- What happens if you try to `git push origin main` when Branch Protection requires a Pull Request?