World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Make and switch branches

You will learn: how to hop onto a branch, and make one in one step.

The problem

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

You made a dark-mode note. But the star is still on main. How do you hop over?

Watch

HEAD jumps from main to the dark-mode note. Both notes sit on the same commit.
6d3a9b1a8feb63270a76e

git switch dark-mode moves HEAD onto that note. Your new commits now go on dark-mode. main stays safe.

Try it

Hop onto the dark-mode branch.

6d3a9b1a8feb63270a76e

Watch

A new note, about, appears on the same commit, and HEAD hops onto it in one step.
6d3a9b1a8feb63270a76e

In a hurry? git switch -c about makes a new note and hops on. Two steps in one.

Try it

Two branches grew apart. Think of them as two universes. Hop into each one and watch your files.

Tap a lane, or pick a universe below. Watch the files change.

6d3a9b1a8feb63270a76ea20da6c013a957
Universes (branches). Arrow keys switch too.

Your folder in the main universe

  • README.md
  • index.html
  • style.css

index.html

<h1>My Diary</h1>
<p>Day 1: I learned Git.</p>
<p>Day 2: I learned branches.</p>

Predict

You are on about. You switch to main. What happens to about.html in your folder?

Pick one answer.

Try it

Make a branch called contact and hop onto it, with one command.

6d3a9b1a8feb63270a76e

You did it!

You can now make branches and hop between them.

Go deeper (optional)

git switch - hops back to the branch you were on. Changed files but didn’t commit? Git carries them along when it is safe. If a change would get lost, Git stops and tells you. Lesson 9 shows the fix: stash.

Make contact and hop on, then git switch - hops back to main. Terminal output: $ git switch -c contact Switched to a new branch 'contact' $ git switch - Switched to branch 'main' $ git branch contact dark-mode * main