Unit 5: Branches
Try wild ideas safely.
Unit 5 of 8 in Git and version control for kids. Its 5 lessons are What Is a Branch?, Make One and Hop On, Bringing It Home, Tidying Up and Branch Boss — below is everything each one explains, and a question or two from it to try.
Every terminal session on this page was replayed before it shipped, on a git whose every message is tested against the real one.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🌿 What Is a Branch?
A second timeline
You have a working game. You want to try giving the player a jetpack, which might ruin everything.
Make a branch. It is a separate line of commits that starts from where you are. The version that works stays exactly as it is, on main, untouched.
A branch is just a name for a commit
This is the secret that makes branches easy: a branch is a sticky label on a commit, and it moves forward every time you commit.
That is why making one is instant. Nothing is copied.
Terminal
$ git branch * main $ git branch jetpack $ git branch jetpack * main
Try it yourself
What does the * mean in git branch?
- That branch is broken
- That is the branch you are on right now
- That branch is finished
- That branch is shared with others
How long does it take git to make a branch of a huge project?
- Ages — it copies every file
- No time at all — it just writes down one commit name
- It depends on your internet
- A branch cannot be made from a big project
Answer them in the app
🦘 Make One and Hop On
git switch -c
git switch -c jetpack makes the branch and moves you onto it, which is almost always what you wanted.
After that, every commit you make lands on jetpack. main stays where it was.
Terminal
$ git switch -c jetpack Switched to a new branch 'jetpack' $ echo "jetpack!" >> game.txt $ git add . $ git commit -m "Add a jetpack" [jetpack 8ebdd8d] Add a jetpack 1 file changed, 1 insertion(+) $ git log --oneline 8ebdd8d (HEAD -> jetpack) Add a jetpack 8b57887 (main) Make a game
Hopping back
git switch main moves you back — and the files on your disk change with you. The jetpack line vanishes from game.txt, because on main it was never written.
It is not lost. It is sitting on the other branch, waiting.
Terminal
$ cat game.txt a game jetpack! $ git switch main Switched to branch 'main' $ cat game.txt a game
Try it yourself
You switch to main and your new feature disappears from the file. What happened?
- It was deleted forever
- It is still on the other branch — switch back and it returns
- Git is broken
- It moved to the staging area
Answer it in the app
🔀 Bringing It Home
Merging
The jetpack works. Now you want it on main.
Switch to the branch that should receive the work, then git merge the one that has it. The direction matters: you merge *into* where you are standing.
Terminal
$ git merge jetpack Updating 8b57887..8ebdd8d Fast-forward game.txt | 1 + 1 file changed, 1 insertion(+) $ cat game.txt a game jetpack!
"Fast-forward"
Git said Fast-forward. It means main had not moved at all while you were away, so there was nothing to combine — git just slid the main label forward onto your newest commit.
The simplest possible merge, and the most common one.
Try it yourself
You want the work from fix to end up on main. What do you do?
- git switch fix, then git merge main
- git switch main, then git merge fix
- git merge main fix
- git branch main fix
Answer it in the app
🧽 Tidying Up
Delete the label, keep the work
Once a branch is merged, its label has done its job. git branch -d jetpack removes it.
The commits stay — they are part of main now. You are only throwing away a sticky note.
Terminal
$ git branch -d jetpack Deleted branch jetpack (was 8ebdd8d). $ git branch * main $ git log --oneline 8ebdd8d (HEAD -> main) Add a jetpack 8b57887 Make a game
When git says no
If the branch has commits that are not merged anywhere, -d refuses and tells you so. That refusal has saved a lot of people a lot of work.
It offers -D if you really meant it. Only use -D when you are sure the work is rubbish.
Terminal
$ git branch -d bad-idea error: The branch 'bad-idea' is not fully merged. hint: If you are sure you want to delete it, run 'git branch -D bad-idea'.
Try it yourself
Git refuses to delete a branch, saying it is "not fully merged". What does that mean?
- The branch has commits that exist nowhere else
- The branch is too old
- Someone else is using it
- You spelled the name wrong
Answer it in the app
🏆 Branch Boss
Try it yourself
Why work on a branch instead of straight on main?
- It makes git faster
- main keeps working while you try something risky
- Branches use less space
- Git will not let you commit on main
Answer it in the app