Team workflows · lesson 4 of 6
Commands and ideas in this lesson
AProtected branchrules the host enforces on a branch
BRequired reviews and checksno merging until they pass
CCODEOWNERSwho must review which files
Dgit commit -Sa signed commit

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:

  1. Branch name pattern. Which branches the rule protects, such as main.
  2. Require a pull request. Nobody pushes straight to main; every change is reviewed first.
  3. Required approvals. How many reviewers must approve. Stops “I’ll just merge my own”.
  4. Require status checks. Automated checks (CI) must pass before merging. Stops merging broken work.
  5. Block force pushes. Nobody can rewrite main’s history. Stops lesson 4.7’s disaster.
  6. Do not allow bypassing. The rules apply to admins too. Stops “just this once”.
A branch rule protecting main, labelled. The course’s own drawing; real sites differ in layout and wording.

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:

Terminal
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.

Git sandbox
  1. Commit a change on main and try to push it
  2. Move your commit onto a new branch, fix-sinigang, and push that
  3. Open a pull request from it
  4. 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

  1. 67e435d HEAD →mainorigin/main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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:

.github/CODEOWNERS
# 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 -S signs a commit; git config --global commit.gpgsign true signs every one.
  • Git can sign with a GPG key, or, more simply, with an SSH key: git config --global gpg.format ssh and user.signingkey set 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"

Spot the trapAna

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

Main requires a pull request. What happens to git push straight to main?
Which setting would have stopped lesson 4.7’s forced push from deleting Joaquin’s commit?
What does a Verified badge on a commit tell you?

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.

All sources for Simple Git