Everyone Can Learn Program
中文 Start learning
Core · GitLesson 3 of 6Free to read

Branching and merging · Git and GitHub

How do you try something without breaking the main version?

Course contents

Why you need it

On a team, main should always work. New features and experiments happen on branches: separate lines of history you can merge back when they're ready, or delete if the idea didn't work. Every pull request on GitHub is a branch.

The intuition

A branch is just a label pointing at a commit. When you commit, the label you're on moves forward.

  • git switch -c login: make a branch called login and move to it
  • git switch main: go back; your files change to main's version
  • git merge login: bring login's commits into the branch you are on

If main didn't move while you worked, the merge is a fast-forward: the main label simply slides forward. If both moved, Git creates a merge commit with two parents.

Definition

Branch

A movable name for a line of commits. `main` is just the branch most teams treat as the official one.

Merge

Combining the work of another branch into the current one, by fast-forward or with a merge commit.

Examples

A feature branch, fast-forward

Run it in the lesson

Work on a branch, then merge it into an unchanged main.

git switch -c login
echo "def login(): pass" > login.py
git add login.py
git commit -m "add login"
git switch main
ls
git merge login
ls
Step by step
  1. L1New branch, and you're on it.
  2. L4The commit goes on login only.
  3. L6Back on main, login.py isn't here.README.md app.py
  4. L7main hadn't moved, so it just slides forward.
  5. L8Now main has it too.README.md app.py login.py

Both moved: a merge commit

Run it in the lesson

A teammate's change on main and yours on a branch touch different files, so Git combines them automatically.

git switch -c search
echo "def search(): pass" > search.py
git add search.py
git commit -m "add search"
git switch main
echo "Now with search" >> README.md
git commit -am "update readme"
git merge search
git log --oneline
Step by step
  1. L7main moves on too.
  2. L8Both sides have new commits, so Git makes a merge commit.
  3. L9The merge commit sits on top and has two parents.

Common mistakes

Working directly on main

Unfinished work on main blocks everyone else. Make a branch for anything that takes more than one commit.

Merging the wrong way round

git merge X brings X into the branch you are on. Check git branch (the * marks where you are) before merging.

Quick quiz

1. What does the last command print?

git switch -c dark-mode
git switch -c fonts
git branch
  1. A
      dark-mode
    * fonts
      main
  2. B
    * dark-mode
      fonts
      main
  3. C
      dark-mode
      fonts
    * main
Show the answer

Answer: A

switch -c moves you to the new branch each time, so you end on fonts.

2. You're on `main` and run `git merge feature`. main has no new commits since feature started. What happens?

  1. AA fast-forward: main moves to feature's commit
  2. BA merge commit with two parents
  3. CA conflict
Show the answer

Answer: A

With nothing new on main, there's nothing to combine: the label just moves.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictWhere are the files?

    Type exactly what the last command prints.

  2. ModifyCommit on the right branch

    These commands put the new feature straight onto main. Change them so the commit goes on a new branch called wishlist, and main stays at first version.

  3. WriteFinish a feature

    A branch reviews already has a finished commit. Merge it into main, then delete the branch with git branch -d reviews.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Branch分支A movable name for a line of commits.
Merge合并Bringing another branch's commits into the current one.
Fast-forward快进A merge where the branch label just moves forward.