Skip to content
KALANBLON
Show contents

Chapter 1 of 6 · 20 min

Branches and pull requests

By the end of this chapter, you'll be able to

  • Describe the workflow based on branches and pull requests
  • Create one branch per task and keep it short
  • Open a useful pull request: title, description, screenshots, tests
  • Explain the role of automated checks and of protecting main

The workflow

The most common workflow, often called GitHub flow, fits in six steps:

  1. start from an up-to-date main branch, main;
  2. create a branch for one task: feat/course-search, fix/broken-link;
  3. make small, clear commits on this branch;
  4. push the branch and open a pull request (PR, called a merge request on GitLab): a request to merge that the team can read and comment on;
  5. the automated checks (tests, lint) run, and at least one person reviews;
  6. you merge into main, then delete the branch.

The change lands in main, clean and reviewed.

Step 5 of 5
Text description of the animation
  1. One branch per task, created from an up-to-date main.
  2. Each commit does one thing and says which.
  3. The pull request explains what, why and how to check.
  4. Tests pass automatically; a colleague reviews and approves.
  5. The change lands in main, clean and reviewed.

The commands

git switch main
git pull                                  # main up to date before starting
git switch -c feat/course-search          # new branch for the task
# … changes, then:
git add src/search.js
git commit -m "feat: add search by title"
git push -u origin feat/course-search     # publishes the branch to the remote repository

Next, you open the pull request on the hosting site (GitHub, GitLab…), or from the command line with GitHub's tool: gh pr create.

Try the local cycle in the simulator, all the way to the merge:

Mini-lab: a feature branchGit simulator

Terminal

Click “Run”: the commands run in a fresh repository. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Git commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

A good pull request

A pull request is read by someone who was not inside your head. It must answer three questions:

Title: Add search by title to the catalogue

Why
Students cannot find courses in a list of 50 items (ticket #42).

What changes
- a search field above the list;
- the search ignores case and accents;
- a message appears when no course matches.

How to check
1. Open /catalogue and type "query": the SQL course appears.
2. Type "cobol": the message "No course matches" appears.
Tests added: search.test.js (4 cases).

Keep pull requests small: fewer than 300 changed lines can be reviewed in a quarter of an hour; 2,000 lines are never really reviewed.

Protecting the main branch

On GitHub or GitLab, the team protects main in the repository settings: no direct pushes, at least one approval required, automated checks green before any merge. This is not distrust: it is a safety net that protects everyone, including from their own end-of-day mistakes.

Your turn

Worked exercise

Worked exercise: splitting a task

The team must add a "My account" area to the site: sign-in page, profile page, password change and account deletion. Propose a split into branches and pull requests, with a branch name and a title for each, in the order in which to merge them.

Hint 1
Each pull request must bring something usable or testable on its own, even if the complete feature is not finished.
Hint 2
The prefixes feat/, fix/, docs/ indicate the nature of the branch.
Show the solution and common mistakes
1. feat/sign-in              "Add the sign-in page"
2. feat/profile              "Show the signed-in user's profile"
3. feat/password             "Allow changing your password"
4. feat/account-deletion     "Allow deleting your account with confirmation"
5. docs/my-account           "Document the My account area"

Each pull request is a few hundred lines at most, can be reviewed in one go, and leaves the site working after the merge. Sign-in comes first, because all the other pages depend on it.

Common mistakes:

  • A single feat/my-account branch for everything: a huge pull request, skimmed rather than reviewed.
  • Splitting by file type ("all the HTML", then "all the CSS"): no step is usable on its own, and each pull request either breaks something or is useless.

Summary

  1. 1Nobody changes main directly: everything goes through a short branch and a pull request.
  2. 2A branch matches one task, and is created from an up-to-date main.
  3. 3A good pull request says why, what changes and how to check, and stays small.
  4. 4Automated checks and at least one review come before every merge.
  5. 5Protecting main in the repository settings prevents mistakes for the whole team.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.