Quest 04
Branches
A branch is just a name that points at a commit. That is why they are cheap, fast and nothing to be scared of.
Goal#
Make branches, switch between them, see history as a graph, and merge.
A branch is a sticky note#
main is a label on one commit. When you commit on main, the label moves to the new commit. A new branch is a new label; HEAD says which label you are carrying.
git branch # list branches; * marks yours
git switch -c nap-time # create a branch here and move onto it
git switch main # move back
git log --oneline --graph --all # every branch, drawn as a graph
Make git log --oneline --graph --all a habit. It turns branches from a mystery into a picture.
Two kinds of merge#
git merge treats brings the commits of treats into the branch you are on.
- Fast-forward. If your branch has not moved since
treatsstarted, git just slides your label forward. No new commit. - Three-way merge. If both branches have new commits, git makes a merge commit with two parents, joining the lines in the graph.
Quest 04#
quest start 4
The treats branch is ready. Then HEAD wants to plan nap time on a branch of her own:
- Merge
treatsintomain. - Make a branch called
nap-time, and commit something on it. - Merge
nap-timeintomain, and finish onmain.
Draw the graph after every step. Then quest check.
Check it works#
git log --oneline --graph --allshows both branches insidemain.quest checkpasses.
Exercise#
Make a commit on main and another on a new branch, so both have moved, then merge. Look at the graph: there is your three-way merge commit, with two parents. git show --format=%P -s HEAD prints them.
More to try
- Delete a merged branch with
git branch -d nap-time. The commits stay; only the label goes. - Try
git merge --no-ffon a fast-forward. When would a team want that?
Quest 04
quest start 4 # ...fix it with real git... quest check
Mark it done once quest check shows every line as PASS. Your progress stays in this browser.