learn git.
0/25 done
learn git/branches/course 14 of 25

Branches: parallel universes

Try something risky without breaking the real thing.

8 min read·3 sections·2 tasks
the key idea

A branch is a separate line of history — step onto it, and your commits land in a world where main never moved.

01The problem branches solve

Your main line of work runs fine — the project builds, everything works. Now you want to try a big idea: rewrite the layout, change the data model, refactor something that touches everything. If you do it directly on main and the idea fails halfway, you are stranded: main is broken, the working bits are tangled with the abandoned bits, and unwinding them is manual surgery.

The copy-the-folder trick people use for this — project-experiment beside project — works but forgets one thing: you keep working on the original while experimenting, and the two folders drift apart in silence. Merging them later is a nightmare of which-file-is-newer.

02What a branch really is

Here is the truth that makes branches stop being scary: a branch is not a copy of your project. It is a tiny movable label stuck on one commit — that is all. main is a label pointing at your newest photograph. Creating a branch is writing a second label. It takes no space and no time; that is why professionals create them constantly.

When you commit while standing on a branch, that branch's label slides forward to the new photograph — and the other labels stay exactly where they were. The two lines of history have diverged, each with its own commits, sharing everything that came before the split.

And the trick that makes it feel like magic: the files you see on disk always match the branch you are standing on. Switch branches, and your folder becomes that universe — files appear and vanish to match. One folder, many universes, one switch command away.

  • —a branch = a movable label pointing at a commit
  • —committing moves the label you stand on — only that one
  • —your files on disk always match where you stand
Why they are called branches

Draw the commits as a chain, then split: one line forks into two, like a tree branch. The word is the picture. Every branch is a fork in the timeline, and merges — later in this course — are where forks grow back together.

03The shape of professional work

In real projects, almost nobody commits directly to main. The rhythm is: make a branch for each piece of work, commit freely on it, and when the idea proves out, merge it back into main. main stays working the whole time — it only ever receives finished, tested ideas.

The next three courses walk exactly that road: create a branch, move between branches, and finally merge one home. The commands are almost boringly small — the mental model above is what makes them feel safe.

[bash]· the road ahead, previewed
git branch            # where can I stand?
git switch feature     # step onto feature
git commit -m "experiment"
git switch main        # back to safety
>>

Practice

A one-commit project. See where you can stand.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks1/2 done
  1. 01.List the branches with git branch.

    hint: Type: git branch

  2. 02.Confirm main exists and you are standing on it.