Sikau

Git & GitHub · Day 7 · about 25 min

Branches

Parallel timelines for new ideas.

Try it without breaking it

You want to try a dark theme, but the site must keep working while you experiment. A branch is a separate line of commits: experiment there, and main stays exactly as it was.

A parallel universe: Branching splits the timeline. In one universe you redesign the menu; in the other, nothing changed. Later you can merge the universes — or throw the experiment away.

Create, switch, compare

git branch
git switch -c dark-theme
echo "body { background: #111; }" > style.css
git add style.css
git commit -m "Add dark theme"
git log --oneline
git switch main
ls
git log --oneline --all
  • A branch is just a movable label pointing at a commit. Creating one is instant and free.
  • Name branches after what they do: add-contact-form, fix-price-typo.
  • Commit or stash your changes before switching, or Git may refuse to switch.

The rest of this lesson

  • Branches are just labelsexplain
  • Where are you?predict
  • Files after switchingpredict
  • Branch commandsmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 18 lessons of Git & GitHub