World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

A branch is a sticky note

You will learn: what a branch really is, and how to make one.

The problem

Three commits on main: Add README, Add day 1 page and Add style. The newest one glows and wobbles.

You want to try a dark theme for your diary. But main must keep working. Where can you play safely?

Watch

A new green label, dark-mode, appears on the newest commit, next to main. HEAD stays on main.
6d3a9b1a8feb63270a76e

A branch: A storyline of commits. Each branch has a name, like main. is a sticky note on a commit. git branch dark-mode sticks a new note here. You stay where you are.

Try it

Make a branch called dark-mode.

6d3a9b1a8feb63270a76e

Watch

git branch lists dark-mode and main. A star sits next to main. Terminal output: $ git branch dark-mode $ git branch dark-mode * main

git branch with no name lists your notes. The star marks the one you are on.

Try it

Notes can go on old commits too. Stick a note called idea on “Add day 1 page”.

Tap a commit. Then stick a new branch on it.

6d3a9b1a8feb63270a76e

First, tap a commit on the graph.

Predict

You are on main. You make a new commit. Which note moves forward?

Pick one answer.

Check

After the new commit, main and HEAD move forward to Add day 2 entry. dark-mode stays on Add style.
6d3a9b1a8feb63270a76e

See? Only main moved. HEAD rides along on main. dark-mode waits on the old commit.

You did it!

You can now make branches and see what they really are.

Go deeper (optional)

git branch -v shows the commit under each note. git branch -m dark-mode night-mode renames one. Branch names can’t have spaces. Teams use - and /, like feature/dark-mode. Why are branches so cheap? Inside Git, a branch is just a name holding one commit id.

git branch -v shows dark-mode and main, each with a short hash and the message Add style. Terminal output: $ git branch dark-mode $ git branch -v dark-mode 270a76e Add style * main 270a76e Add style