Branching and merging · Git and GitHub
How do you try something without breaking the main version?
Course contentsWhy 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 itgit switch main: go back; your files change to main's versiongit 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 lessonWork 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- L1New branch, and you're on it.
- L4The commit goes on login only.
- L6Back on main, login.py isn't here.
README.md app.py - L7main hadn't moved, so it just slides forward.
- L8Now main has it too.
README.md app.py login.py
Both moved: a merge commit
Run it in the lessonA 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- L7main moves on too.
- L8Both sides have new commits, so Git makes a merge commit.
- 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 branchShow 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?
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.
- PredictWhere are the files?
Type exactly what the last command prints.
- 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 atfirst version. - WriteFinish a feature
A branch
reviewsalready has a finished commit. Merge it intomain, then delete the branch withgit 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
| Term | Chinese | Definition |
|---|---|---|
| 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. |