Gitastic
Path › Part 3: Working with others › Lesson 10 of 16
10

The feature-branch & pull-request workflow

How most teams actually ship changes, step by step.

1 Learn the idea

Almost every team uses the same loop: nobody commits directly to main. Each change lives on its own feature branch, gets pushed, reviewed in a pull request (PR, called merge request on GitLab), and is merged into main on the server.

The loop

  1. git switch main and git pull: start from the latest version.
  2. git switch -c my-feature: a fresh branch for this one change.
  3. Commit as often as you like.
  4. git push -u origin my-feature, then open a PR on GitHub.
  5. After review, the PR gets merged on origin (the GitHub button).
  6. Back home: git switch main, git pull, git branch -d my-feature.

2 Watch it happen

One feature, from branch to main

Press Play or use → to step through. Watch the graph as each command runs.

origin (GitHub — shared with your team)
220addd Initial commit220adddInitial commit0f80ea7 Add product page0f80ea7Add product pa…Branch "main" — a movable pointer (current branch: moves when you commit)main
your laptop ~/project · on main
220addd Initial commit220adddInitial commit0f80ea7 Add product page0f80ea7Add product pa…HEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainRemote-tracking branch: where origin's main was last time you fetchedorigin/main
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git switch -c dark-mode

    Start a feature branch from an up-to-date main. It is just a new label on the same commit.

  2. $ edit theme.css "body { background: #111 }"
    $ git add theme.css
    $ git commit -m "Add dark theme"

    Work and commit on the branch. main does not move.

  3. $ edit toggle.js "toggleTheme()"
    $ git add toggle.js
    $ git commit -m "Add theme toggle"

    Another commit. Small, focused commits make reviewing easy.

  4. $ git push -u origin dark-mode

    Publish the branch. A new dark-mode appears on origin and your branch now tracks origin/dark-mode. On GitHub you would click "Compare & pull request" now.

  5. $ teammate merge dark-mode

    Sam reviews the PR and clicks Merge. The merge commit is created on origin — look at the top panel. Your laptop does not know yet.

  6. $ git switch main

    Back to main. Locally it still points to the old commit.

  7. $ git pull

    Pull brings the merge commit down. Your main fast-forwards to match origin: the feature is officially in.

  8. $ git branch -d dark-mode

    -d deletes the local branch label. It is safe: git only allows it because the commits are already part of main.

  9. $ git push origin --delete dark-mode

    Tidy up the remote branch too (GitHub has a button for this). The commits stay in history; only the label is gone.

  • git branch -d refuses to delete unmerged work. -D forces it, so use it only when you really mean "throw this away".
  • Name branches after the change: fix-login-timeout, add-dark-mode.
  • Never commit directly to main on a team repo, even for tiny fixes.
git switch -c <feature>Start a feature branch
git push -u origin <feature>Publish it and open a PR
git pull origin mainBring new main commits into your branch
git branch -d <feature>Delete a merged branch locally

3 Check your understanding

Question 1

Where does the merge of a pull request usually happen?

Question 2

Your PR has been merged. What is the usual clean-up?

Question 3

Look at the graph. Can git branch -d feature succeed right now while on main?

220addd Initial commit220adddInitial commit5b1ad69 Feature work5b1ad69Feature workHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainBranch "feature" — a movable pointerfeature
Question 4

While your PR is open, main gets 5 new commits. Which is a reasonable way to update your feature branch?

4 Practice for real

Your mission

Ship a fix through a PR

Fix the README through the full workflow: create branch fix-typo, commit a change, publish it, get it merged (simulate the review with teammate merge fix-typo), update your local main, and delete the local branch.

origin (GitHub — shared with your team)
220addd Initial commit220adddInitial commit0f80ea7 Add product page0f80ea7Add product pa…b95f697 Update pricesb95f697Update pricesBranch "main" — a movable pointer (current branch: moves when you commit)main
your laptop ~/project · on main
220addd Initial commit click to insert the hash into the terminal220adddInitial commit0f80ea7 Add product page click to insert the hash into the terminal0f80ea7Add product pa…HEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainRemote-tracking branch: where origin's main was last time you fetchedorigin/main
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $