Protecting branches
Last updated
So far, anyone who can push to Kusina can push straight to main, force-push it, or merge their own pull request unread. Hosts let a repository's owner set rules on a branch that stop those things. In this lesson you'll read a branch rule setting by setting, have a push refused by one, and meet code owners and signed commits.
By the end of this lesson you can
- Explain what a protected branch rule prevents, setting by setting
- Work with a protected main: branch, pull request, review, merge
- Say what code owners and signed commits add
A branch rule, setting by setting
On the host, in the repository's settings, the owner adds a rule for main. Each setting closes off one mistake from earlier in this course:
- Branch name pattern. Which branches the rule protects, such as main.
- Require a pull request. Nobody pushes straight to main; every change is reviewed first.
- Required approvals. How many reviewers must approve. Stops “I’ll just merge my own”.
- Require status checks. Automated checks (CI) must pass before merging. Stops merging broken work.
- Block force pushes. Nobody can rewrite main’s history. Stops lesson 4.7’s disaster.
- Do not allow bypassing. The rules apply to admins too. Stops “just this once”.
The names differ between hosts (GitHub has branch protection rules and rulesets; GitLab has protected branches and merge request approvals), but the settings are much the same.
Working with a protected main
With main protected, a push straight to it is refused by the host, even though your commit is fine:
git push remote: error: GH006: Protected branch update failed for refs/heads/main. remote: Changes must be made through a pull request. ! [remote rejected] main -> main (protected branch hook declined) error: failed to push some refs to 'https://example.com/ana/kusina.git'
The way through is the one from lesson 5.2: a branch, a pull request, a review.
- Commit a change on main and try to push it
- Move your commit onto a new branch,
fix-sinigang, and push that - Open a pull request from it
- Put your main back to match origin/main
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Remote
origin: example.com/ana/kusina.git · in step, as of your last fetch
You are in ~/kusina. On the hosting site, main is now protected: changes must come through a pull request.
Commit graph
67e435dHEAD →mainorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
On the hosting site: example.com/ana/kusina
Branches: main · protected: main
Hint for the second and fourth tasks: git branch fix-sinigang puts a label on your commit; then git reset --hard origin/main on main takes main back; then git switch fix-sinigang and git push -u origin fix-sinigang.
Code owners
A CODEOWNERS file (in the repository root, docs/ or .github/) says who is responsible for which files. With "require review from code owners" on, a pull request touching those files needs their approval:
# Lola Remy reviews every recipe; Joaquin owns the index script. *.md @lola-remy desserts/ @lola-remy @carla build_index.py @joaquin
The host requests their review automatically when a pull request touches their files.
Signed commits
Anyone can set user.name to "Lola Remy" (lesson 1.3): a commit's author is just text. A signed commit adds a cryptographic signature made with a key only you hold, so the host can show it as Verified: it really came from that key's owner.
git commit -Ssigns a commit;git config --global commit.gpgsign truesigns every one.- Git can sign with a GPG key, or, more simply, with an SSH key:
git config --global gpg.format sshanduser.signingkeyset to your public key's file. You then add the key to your host account as a signing key, following its guide.
A branch rule can require signed commits. Simple Cybersecurity covers how such signatures work.
Spot the trap: "bypass, just in case"
Ana protects main, then ticks “Allow administrators to bypass” and makes Joaquin and Carla admins too, “in case something urgent comes up”.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- GitHub Docs: About protected branches: required reviews, status checks, force pushes and bypassing
- GitHub Docs: About code owners: the CODEOWNERS file
- GitLab Docs: Protected branches: the same on GitLab
- Git documentation: git-commit, -S: signing commits with GPG or SSH keys
- GitHub Docs: About commit signature verification: the Verified badge