Branch Protection
Time: ~4 minutes | Difficulty: Beginner
What You'll Learn
- What branches are
- Why protect the main branch
- How to set up basic protection
The Big Idea
Branches let you work on features without affecting the main code. Branch protection prevents accidental damage to your production code.
Git Branches Explained
main ──●──●──●──●──●──●────────────────●
\ /
●──●──●──●──────────── feature branch
feature work- main: The stable, production-ready code
- feature branches: Where you experiment and develop
When the feature is ready, you merge it back to main.
Why Protect Main?
Without protection:
- You might accidentally push broken code
- Force pushes can overwrite history
- No review before code goes live
With protection:
- Changes must go through pull requests
- Required checks must pass
- History is preserved
For This Bootcamp
For a solo learning project, we'll set up light protection:
What We'll Enable
- ✅ Require pull request before merging
- ✅ Block force pushes
What We'll Skip (for now)
- ❌ Required reviews (solo project)
- ❌ Required status checks (we'll add CI later)
Setting Up Protection
Step 1: Go to Repository Settings
- Open your repository on GitHub
- Click Settings tab
- In sidebar, click Branches
Step 2: Add Rule
- Click Add branch ruleset (or "Add rule" if using older interface)
- Name:
main protection - Enforcement status: Active
Step 3: Configure Rules
Under "Target branches"
- Add target:
main
Under "Rules"
- ✅ Restrict deletions
- ✅ Block force pushes
- ✅ Require a pull request before merging
- Required approvals: 0 (solo project)
Step 4: Save
Click Create or Save changes
Working with Branches
Now that main is protected, here's the workflow:
Create a Feature Branch
bash
# Create and switch to new branch
git checkout -b feature/add-login
# Do your work...
git add .
git commit -m "Add login form"
# Push the branch
git push -u origin feature/add-loginCreate Pull Request
- Go to GitHub
- You'll see a prompt to create a PR
- Click Compare & pull request
- Add a title and description
- Click Create pull request
Merge (After Review)
- Review the changes
- Click Merge pull request
- Click Confirm merge
- Delete the branch (optional but clean)
Update Local Main
bash
git checkout main
git pullSimplified Workflow for Learning
For this bootcamp, a simpler approach:
- Work directly on main for initial setup
- Use feature branches for larger features
- Pull requests for important changes
We won't enforce strict branch workflow since you're learning. The protection prevents accidents, not normal work.
Quick Reference
bash
# Create branch
git checkout -b feature/name
# Push branch first time
git push -u origin feature/name
# Switch back to main
git checkout main
# Update main from GitHub
git pull
# Delete local branch after merging
git branch -d feature/nameCheck Your Understanding
- [ ] Branches let you work without affecting main
- [ ] Protection prevents accidental damage
- [ ] Pull requests are how you merge to main
- [ ] I've set up basic protection rules
Next Up
Let's add GitHub Actions to automatically check your code.