This is a free preview. Enrol on the course to save your progress and open every lesson.

Lesson 18 of 28 · 15 min

GitHub Flow and protected branches

How most teams use GitHub

GitHub Flow is simple, and used by teams of every size:

  1. main always works and can be released at any time
  2. Every change gets a short-lived branch from main
  3. Push and open a pull request early (a draft PR if it's not ready)
  4. Automated tests run and a teammate reviews
  5. Merge into main, delete the branch, release

Protect main

Rules can make GitHub enforce this, so nobody (including you on a bad day) pushes straight to main or force-pushes over it. In your repo: Settings → Rules → Rulesets → New ruleset → New branch ruleset. (Older repos may also show Settings → Branches → Branch protection rules, which does the same job.)

  • Target branches: add the default branch (main)
  • Restrict deletions and Block force pushes
  • Require a pull request before merging, with at least 1 approval
  • Require status checks to pass — your GitHub Actions tests (Module 6)
  • Set Enforcement status to Active and save

Merge options

ButtonResult on main
Create a merge commitAll the branch's commits, plus a merge commit
Squash and mergeOne commit containing the whole PR — tidy, popular for small PRs
Rebase and mergeThe branch's commits replayed onto main, no merge commit

Teams pick one and stick to it (Settings → General → Pull Requests).

Try it

Add a ruleset to your portfolio repo that blocks force pushes and requires a pull request. Then try git push straight to main — what happens?